简体   繁体   English

在容器网络中为 ASP .NET WebApp 设置 Nginx 时出现 502 错误网关

[英]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.

相关问题 上传大文件时 502 Bad Gateway nginx 和 ASP.NET Core 3.1 - 502 Bad Gateway nginx and ASP.NET Core 3.1 when upload big file ASP.Net 请求引发 502 Bad Gateway 或 TimeOut - ASP.Net request raises 502 Bad Gateway or TimeOut ASP.NET Core 2.0 ngrok 502 错误网关错误 - ASP.NET Core 2.0 ngrok 502 Bad Gateway Error 502 错误网关 nginx/1.14.1 elasticbeanstalk .net 核心应用程序在 docker 上运行 - 502 Bad Gateway nginx/1.14.1 elasticbeanstalk .net core app running on docker 从另一个 locahost.Net Core API 调用 locahost.Net Core API 时出现错误网关(502)错误 - Bad Gateway (502) error while calling locahost .Net Core API from another locahost .Net Core API 如何在ASP.NET MVC 5中调用SOAP Web服务时修复502 Bad Gateway错误? - How to fix 502 Bad Gateway error when calling SOAP webservice in ASP.NET MVC 5? .net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK - .net core HTTPS requests returns 502 bad gateway while Postman returns 200 OK 在 ASP.NET Web api 控制器中为单元测试设置模拟存储库时返回错误请求 - Returning a Bad Request while setting up mock repository in an ASP.NET web api controller for a unit test 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 使用ngrok 502 Bad Getaway设置电报Bot Webhook - setting up telegram bot webhook with ngrok 502 Bad Getaway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM