【问题标题】:Setup .Netcore app with Nginx fails - getting 502 bad request使用 Nginx 设置 .Netcore 应用程序失败 - 收到 502 错误请求
【发布时间】:2020-05-16 12:57:29
【问题描述】:

我有一个通过容器运行的 .Netcore 应用程序。我需要使用 Nginx 作为反向代理来托管这个容器。所以我有一个 nginx 容器配置为充当反向代理,将流量转发到我的 .netcore 应用程序容器。问题是我在使用 curl 检查我的应用时不断收到 502 bad request。当我检查 nginx 容器日志时,我得到如下信息:

2020/05/10 11:35:51 [error] 6#6: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: vs.local, request: "GET /vs.local HTTP/1.1", upstream: "http://127.0.0.1:5000/vs.local", host: "vs.local"

我已经阅读了很多文章和解决方案,但不幸的是没有运气。

P.S:我的 default.confDockerfile 用于 .netcore 应用程序如下:

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 5000

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["TestAppForKubernetes/TestAppForKubernetes.csproj", "TestAppForKubernetes/"]
RUN dotnet restore "TestAppForKubernetes/TestAppForKubernetes.csproj"
COPY . .
WORKDIR "/src/TestAppForKubernetes"
RUN dotnet build "TestAppForKubernetes.csproj" -c Release -o /app/build
ENV ASPNETCORE_URLS http://*:80

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

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

Nginx default.conf:

server {
    listen 80;
    listen [::]:80;
    server_name vs.local;

    location / {
        root /usr/share/nginx/html;
        index index.html;
}
    location /vs.local {
        proxy_pass http://localhost:5000;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;

        proxy_redirect off;
    }
}

你能帮我一下吗?我被困住了! 谢谢

【问题讨论】:

    标签: nginx .net-core nginx-location nginx-reverse-proxy


    【解决方案1】:

    我找到了答案。 诀窍是你必须在反向代理配置中定义你的 docker gw。 所以而不是:

    proxy_pass http://localhost:5000;
    

    proxy_pass 将是我的情况:

    proxy_pass http://172.18.0.1:5000;
    

    当然,如果 dotnet 应用程序未在 5000 上侦听,则另一个更改是更改端口。

    祝你好运

    【讨论】:

      猜你喜欢
      • 2018-02-04
      • 1970-01-01
      • 2022-01-02
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      相关资源
      最近更新 更多