用Nginx设置.Netcore应用失败--收到502坏请求。

问题描述 投票:0回答:1

我有一个.Netcore应用程序,它通过一个容器运行,我需要用Nginx作为反向代理来托管这个容器。我需要用Nginx作为反向代理来托管这个容器,所以我配置了一个nginx容器作为反向代理,将流量转发到我的.netcore应用容器。所以我配置了一个nginx容器作为反向代理,将流量转发到我的.netcore应用容器。问题是,我一直得到 502错误请求 当我检查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.confDocker文件 .netcore应用的Docker文件如下。

Docker文件。

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个回答
0
投票

我已经找到了答案.诀窍是你必须在反向代理配置中定义你的docker gw.So代替。

proxy_pass http://localhost:5000;

的proxy_pass将是在我的情况下。

proxy_pass http://172.18.0.1:5000;

当然,还有一个变化是改变端口,如果dotnet应用程序不听5000。

祝您好运

© www.soinside.com 2019 - 2024. All rights reserved.