docker 反向代理:无法使 jwilder/nginx 反向代理与多个桥接网络一起工作
docker reverse proxy : can't get jwilder/nginx reverse proxy work with serveral bridge networks
我正在尝试在一个 VPS(基于 prestashop)上拥有多个网站,并且我正在使用 docker 来管理整个事情。 VPS 是 运行ning DEBIAN 10.
我先有一个jwilder/nginx-proxy容器,这里是docker-compose.yml:
version: '3.8'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: c_reverse_proxy
ports:
- 80:80
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- net_reverse_proxy
networks:
net_reverse_proxy:
driver: bridge
然后我想使用以下 docker-compose.yml 动态创建 prestashops 的实例,由 bash 脚本创建并执行作为参数传递的子域的函数:
docker-compose.yml 对于子域“testben”:
version: '3.8'
services:
db:
image: mysql:5.7
container_name: c_mysql_testben
restart: always
volumes:
- /home/ben/shops/testben/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: AfeeRt0
networks:
- net_testben
presta:
image: prestashop/prestashop
container_name: c_presta_testben
expose:
- 80
restart: always
volumes:
- /home/ben/shops/testben/www:/var/www/html
- /home/ben/shops/scripts/post_inst:/tmp/post-install-scripts
environment:
DB_SERVER: c_mysql_testben
DB_USER: root
DB_PASSWD: xxxxxxx
PS_INSTALL_AUTO: 1
PS_ERASE_DB: 0
PS_LANGUAGE: fr
PS_COUNTRY: FR
PS_FOLDER_ADMIN: _admin
PS_DOMAIN: testben.domain.com
VIRTUAL_HOST: testben.domain.com
depends_on:
- db
networks:
- net_testben
networks:
net_testben:
driver: bridge
然后我将nginx反向代理容器连接到新创建的桥接网络:
docker network connect testben_net_testben c_reverse_proxy
到这个阶段,一切都已连接,我进入网络浏览器!
这不起作用,我在尝试访问时进入 firefox testben.domain.com
502 Bad Gateway nginx/1.17.6
以下是一些调试信息:
> docker logs c_reverse_proxy
dockergen.1 | 2020/08/07 16:01:04 Received event start for container 0cb8bc80f4fd
dockergen.1 | 2020/08/07 16:01:04 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
2020/08/07 16:01:18 [notice] 50#50: signal process started
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
dhparam generation complete, reloading nginx
nginx.1 | 2020/08/07 16:01:26 [error] 51#51: *1 no live upstreams while connecting to upstream, client: XXX.XXX.XXX.XXX, server: testben.domain.com, request: "GET / HTTP/1.1", upstream: "http://testben.domain.com/", host: "testben.domain.com"
nginx.1 | testben.domain.com XXX.XXX.XXX.XXX - - [07/Aug/2020:16:01:26 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"
反向代理自动生成配置:
> docker exec c_reverse_proxy cat /etc/nginx/conf.d/default.conf
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
default $http_x_forwarded_proto;
'' $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
default $http_x_forwarded_port;
'' $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
default upgrade;
'' close;
}
# Apply fix for very long server names
server_names_hash_bucket_size 128;
# Default dhparam
ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
default off;
https on;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
resolver 127.0.0.11;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
server_name _; # This is just an invalid value which will never trigger on a real hostname.
listen 80;
access_log /var/log/nginx/access.log vhost;
return 503;
}
# http://testben.domain.com
upstream http://testben.domain.com {
# Cannot connect to network of this container
server 127.0.0.1 down;
}
server {
server_name http://testben.domain.com;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location / {
proxy_pass http://testben.domain.com;
}
}
我认为问题出在:
upstream http://testben.domain.com {
# Cannot connect to network of this container
server 127.0.0.1 down;
}
好像接近this open issue
我在这里:
- DNS 配置良好
- prestashop docker-compose.yml 很好,如果我添加端口:- 8080:80,我可以在没有代理的情况下访问 te 站点
- nginx 容器连接到专用桥接网络,可以看到 prestashop 容器启动
我会接受任何帮助、线索和建议。
我希望避免的是将所有容器都放在一个桥接网络上,因为我希望在此服务器上 运行 的每个 prestashop 实例之间有最大的分离。
感谢您抽出时间阅读。
好的,我通过 my-self 找到了解决方案。发帖帮助别人!
问题是我在创建要代理的容器后将单独的网络 (testben_net_testben
) 连接到 nginx 反向代理容器。所以在创建prestashop容器时,反向代理容器读取容器创建的socket,但是此时生成default.conf时,无法到达prestashop容器
解决方法是先创建网络,连接到反向代理,然后启动主服务,参考外部创建的网络。
第 1 步:
docker network create net_testben
第 2 步:
docker network connect net_testben c_reverse_proxy
第 3 步:
docker-compose up -d
与以下 docker-compose.yml
services:
db:
image: mysql:5.7
container_name: c_mysql_testing
restart: always
volumes:
- /home/ben/shops/testben/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: AfeeRt0
networks:
- net_testben
presta:
image: prestashop/prestashop
container_name: c_presta_testing
expose:
- 80
restart: always
volumes:
- /home/ben/shops/testben/www:/var/www/html
- /home/ben/shops/scripts/post_inst:/tmp/post-install-scripts
environment:
DB_SERVER: c_mysql_testing
DB_USER: root
DB_PASSWD: xxxx
PS_INSTALL_AUTO: 1
PS_ERASE_DB: 0
PS_LANGUAGE: fr
PS_COUNTRY: FR
PS_FOLDER_ADMIN: _admin
PS_DOMAIN: testben.domain.com
VIRTUAL_HOST: testben.domain.com
depends_on:
- db
networks:
- net_testben
networks:
net_testben:
external : true
我正在尝试在一个 VPS(基于 prestashop)上拥有多个网站,并且我正在使用 docker 来管理整个事情。 VPS 是 运行ning DEBIAN 10.
我先有一个jwilder/nginx-proxy容器,这里是docker-compose.yml:
version: '3.8'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: c_reverse_proxy
ports:
- 80:80
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- net_reverse_proxy
networks:
net_reverse_proxy:
driver: bridge
然后我想使用以下 docker-compose.yml 动态创建 prestashops 的实例,由 bash 脚本创建并执行作为参数传递的子域的函数:
docker-compose.yml 对于子域“testben”:
version: '3.8'
services:
db:
image: mysql:5.7
container_name: c_mysql_testben
restart: always
volumes:
- /home/ben/shops/testben/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: AfeeRt0
networks:
- net_testben
presta:
image: prestashop/prestashop
container_name: c_presta_testben
expose:
- 80
restart: always
volumes:
- /home/ben/shops/testben/www:/var/www/html
- /home/ben/shops/scripts/post_inst:/tmp/post-install-scripts
environment:
DB_SERVER: c_mysql_testben
DB_USER: root
DB_PASSWD: xxxxxxx
PS_INSTALL_AUTO: 1
PS_ERASE_DB: 0
PS_LANGUAGE: fr
PS_COUNTRY: FR
PS_FOLDER_ADMIN: _admin
PS_DOMAIN: testben.domain.com
VIRTUAL_HOST: testben.domain.com
depends_on:
- db
networks:
- net_testben
networks:
net_testben:
driver: bridge
然后我将nginx反向代理容器连接到新创建的桥接网络:
docker network connect testben_net_testben c_reverse_proxy
到这个阶段,一切都已连接,我进入网络浏览器!
这不起作用,我在尝试访问时进入 firefox testben.domain.com
502 Bad Gateway nginx/1.17.6
以下是一些调试信息:
> docker logs c_reverse_proxy
dockergen.1 | 2020/08/07 16:01:04 Received event start for container 0cb8bc80f4fd
dockergen.1 | 2020/08/07 16:01:04 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
2020/08/07 16:01:18 [notice] 50#50: signal process started
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
dhparam generation complete, reloading nginx
nginx.1 | 2020/08/07 16:01:26 [error] 51#51: *1 no live upstreams while connecting to upstream, client: XXX.XXX.XXX.XXX, server: testben.domain.com, request: "GET / HTTP/1.1", upstream: "http://testben.domain.com/", host: "testben.domain.com"
nginx.1 | testben.domain.com XXX.XXX.XXX.XXX - - [07/Aug/2020:16:01:26 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"
反向代理自动生成配置:
> docker exec c_reverse_proxy cat /etc/nginx/conf.d/default.conf
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
default $http_x_forwarded_proto;
'' $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
default $http_x_forwarded_port;
'' $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
default upgrade;
'' close;
}
# Apply fix for very long server names
server_names_hash_bucket_size 128;
# Default dhparam
ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
default off;
https on;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
resolver 127.0.0.11;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
server_name _; # This is just an invalid value which will never trigger on a real hostname.
listen 80;
access_log /var/log/nginx/access.log vhost;
return 503;
}
# http://testben.domain.com
upstream http://testben.domain.com {
# Cannot connect to network of this container
server 127.0.0.1 down;
}
server {
server_name http://testben.domain.com;
listen 80 ;
access_log /var/log/nginx/access.log vhost;
location / {
proxy_pass http://testben.domain.com;
}
}
我认为问题出在:
upstream http://testben.domain.com {
# Cannot connect to network of this container
server 127.0.0.1 down;
}
好像接近this open issue
我在这里:
- DNS 配置良好
- prestashop docker-compose.yml 很好,如果我添加端口:- 8080:80,我可以在没有代理的情况下访问 te 站点
- nginx 容器连接到专用桥接网络,可以看到 prestashop 容器启动
我会接受任何帮助、线索和建议。 我希望避免的是将所有容器都放在一个桥接网络上,因为我希望在此服务器上 运行 的每个 prestashop 实例之间有最大的分离。
感谢您抽出时间阅读。
好的,我通过 my-self 找到了解决方案。发帖帮助别人!
问题是我在创建要代理的容器后将单独的网络 (testben_net_testben
) 连接到 nginx 反向代理容器。所以在创建prestashop容器时,反向代理容器读取容器创建的socket,但是此时生成default.conf时,无法到达prestashop容器
解决方法是先创建网络,连接到反向代理,然后启动主服务,参考外部创建的网络。
第 1 步:
docker network create net_testben
第 2 步:
docker network connect net_testben c_reverse_proxy
第 3 步:
docker-compose up -d
与以下 docker-compose.yml
services:
db:
image: mysql:5.7
container_name: c_mysql_testing
restart: always
volumes:
- /home/ben/shops/testben/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: AfeeRt0
networks:
- net_testben
presta:
image: prestashop/prestashop
container_name: c_presta_testing
expose:
- 80
restart: always
volumes:
- /home/ben/shops/testben/www:/var/www/html
- /home/ben/shops/scripts/post_inst:/tmp/post-install-scripts
environment:
DB_SERVER: c_mysql_testing
DB_USER: root
DB_PASSWD: xxxx
PS_INSTALL_AUTO: 1
PS_ERASE_DB: 0
PS_LANGUAGE: fr
PS_COUNTRY: FR
PS_FOLDER_ADMIN: _admin
PS_DOMAIN: testben.domain.com
VIRTUAL_HOST: testben.domain.com
depends_on:
- db
networks:
- net_testben
networks:
net_testben:
external : true