[英]502 Bad Gateway while setting up Nginx for an ASP .NET WebApp in a Container Network
I recently followed a guide to get started with ASP .NET and created a simple WebApp.我最近按照指南开始使用 ASP .NET 并创建了一个简单的 WebApp。
Then I discovered Docker and from now on I would like to Dockerize of all of my services.然后我发现了 Docker,从现在开始我想对我所有的服务进行 Dockerize。
My idea is to run one container with Nginx and one container with my ASP .NET Webapp to route and manage incoming traffic.我的想法是用 Nginx 运行一个容器,用我的 ASP .NET Webapp 运行一个容器来路由和管理传入流量。
The containers themselves are running just fine.容器本身运行得很好。 I can also access the WebApp through its exposed port but accessing it through Nginx with port
80
returns:我还可以通过其公开的端口访问 WebApp,但通过端口
80
Nginx 访问它返回:
502 Bad Gateway.
502错误的网关。
I have read that this may be due to Nginx not knowing the WebApp container but I have checked that they are in the same network.我读到这可能是由于 Nginx 不知道 WebApp 容器,但我已经检查过它们是否在同一网络中。
docker logs nginx_container returns this when I call localhost:80:当我调用 localhost:80 时,docker logs nginx_container 返回这个:
2020/01/14 13:48:18 [error] 6#6: *12 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "localhost"
2020/01/14 13:48:18 [error] 6#6: *12 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "localhost"
2020/01/14 13:48:18 [error] 6#6: *12 no live upstreams while connecting to upstream, client: 172.21.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "localhost", referrer: "http://localhost/"
172.21.0.1 - - [14/Jan/2020:13:48:18 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
I am guessing that it is some kind of configuration that I am getting wrong or do not understand properly.我猜这是某种配置,我弄错了或没有正确理解。 Most of the WebApp configuration was auto-generated by VS.
大多数 WebApp 配置是由 VS 自动生成的。
WebApp Dockerfile: WebApp Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 4000
EXPOSE 4500
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["RazorPagesMovie.csproj", "./"]
RUN dotnet restore "RazorPagesMovie.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "RazorPagesMovie.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RazorPagesMovie.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RazorPagesMovie.dll"]
Nginx Dockerfile: Nginx Dockerfile:
FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
Nginx configuration: nginx配置:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream web-api {
server razorpagesmovie:4000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://172.21.0.1:4000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Docker-Compose: Docker-撰写:
version: '3.4'
services:
reverseproxy:
build:
context: ./Nginx
dockerfile: Nginx.Dockerfile
ports:
- "80:80"
restart: always
razorpagesmovie:
image: ${DOCKER_REGISTRY-}razorpagesmovie
build:
context: .
dockerfile: Dockerfile
depends_on:
- reverseproxy
restart: always
Docker-Compose-Override: Docker-Compose-Override:
version: '3.4'
services:
razorpagesmovie:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:4000;http://+:4500
ports:
- "4000:4000"
- "4500:4500"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
I fixed the problem by editing the Nginx configuration .我通过编辑Nginx 配置解决了这个问题。 As I suspected there was some problem with it.
因为我怀疑它有一些问题。 After thoroughly reading the docs on Nginx again I think I understand how it works now.
再次彻底阅读 Nginx 上的文档后,我想我现在明白它是如何工作的了。
The updated Nginx configuration:更新的 Nginx 配置:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream rzpage {
server razorpagesmovie:4000;
}
server {
listen 80;
server_name rzpage;
location / {
proxy_pass http://razorpagesmovie:4000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
I am unsure if I should edit my answer into my question, so I will leave it as is for now.我不确定是否应该将我的答案编辑到我的问题中,所以我现在将其保留原样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.