nginx反向代理不适用于asp.net核心docker应用

时间:2020-08-06 10:10:59

标签: nginx docker-compose asp.net-core-3.1

我在docker容器中有一个asp.net Web API应用程序,并且可以通过以下命令正常工作,

 docker build -t hello-aspnetcore3 -f Api.Dockerfile .
 docker run -d -p 5000:5000 --name hello-aspnetcore3  hello-aspnetcore3

浏览至:http:// localhost:5000 / weatherforecast完美。

现在我正尝试使用Nginx作为反向代理并在另一个容器中运行,这里是nginx conf和容器文件,

nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http {

sendfile on;

upstream web-api {
    server api:5000;
}

server {
    listen 80;
    server_name $hostname;
    location / {
        proxy_pass         http://web-api;
        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;
    }
  }
}

Nginx.Dockerfile

FROM nginx:latest

COPY nginx.conf /etc/nginx/nginx.conf

这是我的docker compose文件,

docker-compose.yml

version: "3.7"

services:

reverseproxy:
build:
  context: ./Nginx
  dockerfile: Nginx.Dockerfile
ports:
  - "80:80"
restart: always

api:
depends_on:
  - reverseproxy
build:
  context: ./HelloAspNetCore3.Api
  dockerfile: Api.Dockerfile
expose:
  - "5000"
restart: always

构建并运行容器可以使用docker-compose builddocker-compose up -d

但是,当尝试浏览http://localhost/weatherforecast时,会出现HTTP Error 404. The requested resource is not found.错误。怎么了?

注意-当我使用主机ip地址http://192.168.0.103/weatherforcast浏览时,就可以了。

Docker-Compose ps输出在这里。...

enter image description here

0 个答案:

没有答案
相关问题