我有一個包含 3 個容器的 docker 環境:前端(角度)、后端(dotnet)和nginx。
我正在嘗試使用proxy_pass配置 nginx 以將/api位置從容器之一定向到我的 API。
這是我的 nginx 配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {
proxy_pass http://sitr-api:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
這些是我的 Dockerfile:
API點網:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY . .
EXPOSE 80
ENTRYPOINT ["dotnet", "SITR.Web.Host.dll", "--environment=Staging"]
前端角度:
FROM nginx
COPY . /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d
EXPOSE 80
nginx
FROM nginx
COPY default_nginx.conf /etc/nginx/conf.d/default.conf
我的 docker-compose 檔案
version: '3.0'
services:
sitr-api:
image: sitr-api
container_name: sitr-api
environment:
ASPNETCORE_ENVIRONMENT: Staging
ports:
- "9901:80"
volumes:
- "./Host-Logs:/app/App_Data/Logs"
sitr-app:
image: sitr-app
container_name: sitr-app
ports:
- "9902:80"
nginx:
image: sitr-nginx
container_name: sitr-nginx
depends_on:
- sitr-app
- sitr-api
ports:
- "81:80"
該容器的作業,因為我能夠訪問本地主機:9901(后端)和本地主機:9902(前端)。
我的前端通過 localhost:81 上的 nginx 訪問也可以正常作業,但是到我的localhost:81/api后端的 proxy_pass不起作用(http 404)。
我的 nginx 配置有什么問題?
uj5u.com熱心網友回復:
我正在根據@super 的評論發布回復。根據他的評論,只需要在他重定向時進行重寫以洗掉/api。
我需要使用正則運算式添加以洗掉/api的特定行是:
rewrite ^/api(/.*)$ $1 break;
這是完整的 nginx 配置:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sitr-app:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {
rewrite ^/api(/.*)$ $1 break;
proxy_pass http://sitr-api:80;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/315304.html
