【问题标题】:What's wrong with my nginx configuration with docker?我的 nginx 配置与 docker 有什么问题?
【发布时间】: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


【解决方案1】:

我根据@super 的评论发布回复。 根据他的评论,只需要在重定向时进行 rewrite 以删除 /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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2018-01-09
    • 2017-03-08
    • 2014-01-13
    • 2014-04-11
    • 2011-09-11
    • 2013-07-17
    相关资源
    最近更新 更多