我在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 build
和docker-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
输出在这里。...