我想设置一个 Nginx 反向代理,它工作正常,但如果我设置network_mode: "host"
它会停止工作,因为它无法找到其他 docker 容器的主机名。我有一个web
容器和一个nginx
容器。
我收到以下错误:
reverseproxy_1 | nginx: [emerg] host not found in upstream "web:80" in /etc/nginx/nginx.conf:10
我的 Nginx 配置文件是:
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-web {
server web:80;
}
server {
listen 8080;
location / {
proxy_pass http://docker-web;
proxy_redirect off;
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-Host $server_name;
}
}
}
我的 docker-compose.yml 文件是:
version: '2'
services:
redis:
image: "redis:alpine"
web:
depends_on:
- redis
build: .\app
volumes:
- .\app:/code
restart: always
reverseproxy:
image: reverseproxy
network_mode: "host"
ports:
- 8080:8080
depends_on:
- web
我需要将 network_mode 设置为 host 否则X-Forwarded-For
会出错。