【发布时间】:2017-10-11 23:27:35
【问题描述】:
我在尝试让以下内容在 Docker 中工作时遇到问题
我想要的是,当用户请求 http://localhost/api 时,NGINX 将反向代理到我在另一个容器中运行的 .Net Core API。
容器主机:Windows
容器 1:NGINX
dockerfile
FROM nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server {
location /api1 {
proxy_pass http://api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
容器 2:.Net Core API
非常简单 - API 暴露在容器的 80 端口上
然后是 docker-compose.yml
docker-compose.yml
version: '3'
services:
api1:
image: api1
build:
context: ./Api1
dockerfile: Dockerfile
ports:
- "5010:80"
nginx:
image: vc-nginx
build:
context: ./infra/nginx
dockerfile: Dockerfile
ports:
- "5000:80"
阅读它指出的 Docker 文档:
链接允许您定义服务的额外别名 可从其他服务访问。他们不需要启用 服务进行通信 - 默认情况下,任何服务都可以到达任何其他服务 以该服务的名称提供服务。
因此,由于我的 API 服务被称为 api1,我只是在 nginx.conf 文件中引用了它作为反向代理配置的一部分:
proxy_pass http://api1;
出现问题,当我输入 http:\\localhost\api 时出现 404 错误。
有没有办法解决这个问题?
【问题讨论】: