我具有以下配置:
reverse_proxy
和app_service
通过app_network
连接由于它们是通过app_network
连接的,所以我可以像app_service
这样在nginx配置中寻址http://app_service:8080
,这很好,因为我不需要公开应用程序服务本身的端口。
[不幸的是,nginx直到我调出应用程序服务容器后才能启动,因为它在启动时会检查主机名app_service
的存在。
[如何防止nginx在启动时检查主机名,在Bad Gateway
尚未运行时尝试连接时可能导致app_service
错误?
供参考的配置文件:
# reverse proxy docker-compose.yml
version: '3'
services:
reverse_proxy:
restart: always
container_name: reverse_proxy
image: nginx
ports:
- 80:80
- 443:443
volumes:
- /srv/docker/nginx/config:/etc/nginx
- /srv/ssl:/srv/ssl
networks:
default:
external:
name: app_network
# application service docker-compose.yml
version: '3'
services:
app_service:
restart: always
container_name: app_service
image: <my_app_image>
networks:
default:
external:
name: app_network
# nginx config
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
location / {
proxy_pass http://app_service:8080;
include proxy.conf;
}
ssl_certificate /srv/ssl/<mycert>.crt;
ssl_certificate_key /srv/ssl/<mykey>.key;
}
这是一种对我有用的方法:
# nginx config
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name app.example.com;
location / {
set $upstream app_service:8080;
proxy_pass $upstream;
include proxy.conf;
}
ssl_certificate /srv/ssl/.crt;
ssl_certificate_key /srv/ssl/.key;
}
如果主机不可用并且在启动时不检查可用性,则会显示502 Bad Gateway
消息。