【发布时间】:2021-11-29 05:31:08
【问题描述】:
我有一个包含 3 个容器的 docker 环境:frontend(角度)、backend(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"
容器正在工作,因为我能够访问 localhost:9901(后端)和 localhost:9902(前端)。
我的前端通过 localhost:81 上的 nginx 访问也可以正常工作,但我的 localhost:81/api 后端的 proxy_pass 无法正常工作 (http 404)。
我的 nginx 配置有什么问题?
【问题讨论】:
-
您的后端是否监听
/api路由?你的 nginx 配置没有重写,所以localhost:81/api将作为http://sitr-ap:80/api传递到后端。通常,您需要在proxy_pass之前添加类似rewrite ^/api(/.*)$ $1 break;的内容,以在传递之前摆脱领先的/api。 -
@super 成功了,就是这样。如果你能把它作为答案,我会在这里接受!
标签: docker nginx docker-compose nginx-reverse-proxy