问题
I am running docker swarm with 1 manager and 2 worker nodes (centos VMs). Each worker node has 1 stack that has an nginx and php service that load "This is site1" in the browser.
The site will only be loaded if the worker node has a reverse proxy (but it will always connect to the stack on that worker node which is part of the problem) but If I run the service on manager node that doesn't have the nginx/php stack it won't automatically direct to the stacks on worker nodes even though I have created an overlay "reprox" network that the stack and reverse proxy is connected to.
docker-compose.yml for the stack:
version: '3'
services:
nginx:
image: nginx:latest
deploy:
placement:
constraints:
- node.role==worker
mode: global
volumes:
- "./code:/code"
- "./site.conf:/etc/nginx/conf.d/site.conf"
networks:
- site1
- reprox
php:
image: php:7-fpm
deploy:
placement:
constraints:
- node.role==worker
mode: global
volumes:
- "./code:/code"
networks:
- site1
networks:
reprox:
external: true
site1:
docker-compose.yml for reverse-proxy (when I run this service on worker nodes it works by typing site1.local in the browser which is defined in my /etc/hosts as the IP address of the node):
version: '3'
services:
nginx:
image: nginx:latest
deploy:
placement:
constraints:
- node.role==manager
mode: global
ports:
- "80:80"
volumes:
- "./conf.d/:/etc/nginx/conf.d"
networks:
- reprox
networks:
reprox:
external: true
conf.d/site1.conf configuration for reverse proxy:
server {
listen 80;
server_name site1.local;
location / {
proxy_pass http://site1_nginx;
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;
}
}
also site.conf for the stack:
server {
listen *:80;
index index.php index.html;
server_name site1.local;
resolver 127.0.0.11;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
set $upstream php:9000;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass $upstream;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
I get this error when running reverse proxy on manager:
nginx: [emerg] host not found in upstream "site1_nginx" in /etc/nginx/conf.d/site1.conf:6
I need this so I can load balance the stacks between nodes, I will also add other stacks that will load different sites, that's why I need reverse-proxy.
I know I can add a separate load balancer on each node that will redirect to reverse proxies on these nodes, but that's too much of an overhead. The reverse-proxy should automatically connect to available stacks in the swarm, that's why I created an overlay network.
来源:https://stackoverflow.com/questions/58393434/reverse-proxy-service-on-manager-node-host-not-found-in-upstream-but-it-works