使用 Nginx 设置 .Netcore 应用程序失败 - 收到 502 错误请求
Setup .Netcore app with Nginx fails - getting 502 bad request
我有一个通过容器运行的 .Netcore 应用程序。我需要使用 Nginx 作为反向代理来托管这个容器。所以我有一个 nginx 容器配置为充当反向代理,将流量转发到我的 .netcore 应用程序容器。问题是我在使用 curl 检查我的应用程序时不断收到 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.conf 和 Dockerfile for .netcore app 如下所示:
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;
}
}
你能帮我一下吗?我卡住了!
谢谢
我找到了答案。
诀窍是你必须在反向代理配置中定义你的 docker gw。
所以代替:
proxy_pass http://localhost:5000;
proxy_pass 将是我的情况:
proxy_pass http://172.18.0.1:5000;
当然,如果 dotnet 应用程序未在 5000 上侦听,另一个更改是更改端口。
祝你好运
我有一个通过容器运行的 .Netcore 应用程序。我需要使用 Nginx 作为反向代理来托管这个容器。所以我有一个 nginx 容器配置为充当反向代理,将流量转发到我的 .netcore 应用程序容器。问题是我在使用 curl 检查我的应用程序时不断收到 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.conf 和 Dockerfile for .netcore app 如下所示:
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;
}
}
你能帮我一下吗?我卡住了! 谢谢
我找到了答案。 诀窍是你必须在反向代理配置中定义你的 docker gw。 所以代替:
proxy_pass http://localhost:5000;
proxy_pass 将是我的情况:
proxy_pass http://172.18.0.1:5000;
当然,如果 dotnet 应用程序未在 5000 上侦听,另一个更改是更改端口。
祝你好运