【问题标题】:Bad Gateway from nginx published with docker-compose来自 nginx 的错误网关使用 docker-compose 发布
【发布时间】:2021-08-13 17:57:08
【问题描述】:

我正在学习 docker-compose,现在我正在尝试在我的 WSL Ubuntu 上的一个 docker-compose 脚本中设置应用程序和 nginx。
我正在测试我的端点

curl -v http://127.0.0.1/weatherforecast

但是我从 nginx 收到 502 Bad Gateway。
如果我在 docker-compose 中更改暴露给端口发布的端口,如下所示,请求会绕过 nginx 并到达我的应用程序,我会收到预期的响应。

ports:
   - 5000:8080

我的设置:
应用的 dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app

ENV ASPNETCORE_URLS=http://+:8080

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["WebApplication2.csproj", "."]
RUN dotnet restore "./WebApplication2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication2.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication2.dll"]

nginx.conf

events {
    worker_connections 1024;
}

http {
    access_log  /var/log/nginx/access.log;

    server {
        listen 80;

        location / {
            proxy_pass http://127.0.0.1:8080/;
        }
    }
}

docker-compose.yml

version: "3.9"
services:
  web:
    depends_on:
      - nginx
    build: ./WebApplication2    
    expose:
      - "8080"
  nginx:
    image: "nginx"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./logs:/var/log/nginx/
    ports:
      - 80:80

>docker-compose ps

       Name                      Command               State                Ports
-----------------------------------------------------------------------------------------------
composetest_nginx_1   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:80->80/tcp,:::80->80/tcp
composetest_web_1     dotnet WebApplication2.dll       Up      8080/tcp

/var/log/nginx/error.log

[error] 31#31: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: , request: "GET /weatherforecast HTTP/1.1", upstream: "http://127.0.0.1:8080/weatherforecast", host: "127.0.0.1"

cURL 输出:

*   Trying 127.0.0.1:80...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET /weatherforecast HTTP/1.1
> Host: 127.0.0.1
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.21.1
< Date: Fri, 13 Aug 2021 17:50:56 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
<
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact

【问题讨论】:

    标签: docker nginx asp.net-web-api docker-compose


    【解决方案1】:

    您应该将您的请求重定向到您的web 容器,而不是127.0.0.1。每个容器都作为网络的独立部分运行(每个都有不同的 IP 地址),127.0.0.1 指向本地容器。因此,在您的情况下,它指向 nginx 本身。可以使用 DNS 名称代替容器的真实 IP 地址(它等于docker-compose 中的服务名称)。使用类似的东西:

    events {
        worker_connections 1024;
    }
    
    http {
        access_log  /var/log/nginx/access.log;
    
        server {
            listen 80;
    
            location / {
                proxy_pass http://web:8080/;
            }
        }
    }
    

    此外,您指定您的 web 容器依赖于 nginx,但反之亦然。喜欢:

    version: "3.9"
    services:
      web:
        build: .
      nginx:
        image: "nginx"
        depends_on:
          - web
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf
        ports:
          - 80:80
    

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多