如何使用 bitnami nginx 容器服务多个容器
How to serve multiple container using bitnami nginx container
我在一台服务器上有容器:
web-zamrud、api-zamrud 和 db-zamrud,它们都使用名为 zamrud-network
的 docker 网桥
web-berlian、api-berlian 和 db-berlian,它们都使用名为 berlian-network
的 docker 网桥
用于 web-zamrud 和 web-berlian 的 nginx 容器。
下面是zamrud容器docker-compose
services:
api-zamrud:
image: registry.gitlab.com/zamrud/zamrudapi
container_name: api-zamrud
networks:
- zamrud-network
web-zamrud:
image: registry.gitlab.com/zamrud/zamrudweb
container_name: web-zamrud
networks:
- zamrud-network
networks:
zamrud-network:
external: true
下面是berlian容器docker-compose
services:
api-berlian:
image: registry.gitlab.com/berlian/berlianapi
container_name: api-berlian
networks:
- berlian-network
web-berlian:
image: registry.gitlab.com/berlian/berlianweb
container_name: web-berlian
networks:
- berlian-network
networks:
berlian-network:
external: true
下面是 bitnami nginx docker compose 文件
services:
nginx:
image: docker.io/bitnami/nginx:1.21
container_name: nginx
volumes:
- ./conf/zamrud.conf:/opt/bitnami/nginx/conf/server_blocks/zamrud.conf:ro
- ./conf/berlian.conf:/opt/bitnami/nginx/conf/server_blocks/berlian.conf:ro
networks:
- zamrud-network
- berlian-network
ports:
- "80:80"
下面是zamrud.conf
server {
listen 80;
listen [::]:80;
server_name zamrud.com www.zamrud.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://web-zamrud;
proxy_redirect off;
}
location /api {
rewrite /api/(.*) / break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api-zamrud:5000;
proxy_redirect off;
}
}
下面是berlian.conf
server {
listen 80;
listen [::]:80;
server_name berlian.com www.berlian.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://web-berlian;
proxy_redirect off;
}
location /api {
rewrite /api/(.*) / break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api-berlian:5000;
proxy_redirect off;
}
}
如果我尝试访问www.zamrud.com,网站内容会正常显示。
但是如果我尝试访问 www.berlian.com,它会显示 [=15= 的内容].
我是否缺少 nginx 中的配置?
如果有人遇到同样的问题,我所做的就是关闭 bitnami nginx 容器
docker-compose down
然后再次启动 bitnami nginx 容器
docker-compose up -d
注:
file name for bitnami nginx docker-compose is docker-compose.yaml
I run the docker down and up in the same folder as docker-compose.yaml
我在一台服务器上有容器:
web-zamrud、api-zamrud 和 db-zamrud,它们都使用名为 zamrud-network
的 docker 网桥web-berlian、api-berlian 和 db-berlian,它们都使用名为 berlian-network
的 docker 网桥用于 web-zamrud 和 web-berlian 的 nginx 容器。
下面是zamrud容器docker-compose
services:
api-zamrud:
image: registry.gitlab.com/zamrud/zamrudapi
container_name: api-zamrud
networks:
- zamrud-network
web-zamrud:
image: registry.gitlab.com/zamrud/zamrudweb
container_name: web-zamrud
networks:
- zamrud-network
networks:
zamrud-network:
external: true
下面是berlian容器docker-compose
services:
api-berlian:
image: registry.gitlab.com/berlian/berlianapi
container_name: api-berlian
networks:
- berlian-network
web-berlian:
image: registry.gitlab.com/berlian/berlianweb
container_name: web-berlian
networks:
- berlian-network
networks:
berlian-network:
external: true
下面是 bitnami nginx docker compose 文件
services:
nginx:
image: docker.io/bitnami/nginx:1.21
container_name: nginx
volumes:
- ./conf/zamrud.conf:/opt/bitnami/nginx/conf/server_blocks/zamrud.conf:ro
- ./conf/berlian.conf:/opt/bitnami/nginx/conf/server_blocks/berlian.conf:ro
networks:
- zamrud-network
- berlian-network
ports:
- "80:80"
下面是zamrud.conf
server {
listen 80;
listen [::]:80;
server_name zamrud.com www.zamrud.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://web-zamrud;
proxy_redirect off;
}
location /api {
rewrite /api/(.*) / break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api-zamrud:5000;
proxy_redirect off;
}
}
下面是berlian.conf
server {
listen 80;
listen [::]:80;
server_name berlian.com www.berlian.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://web-berlian;
proxy_redirect off;
}
location /api {
rewrite /api/(.*) / break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api-berlian:5000;
proxy_redirect off;
}
}
如果我尝试访问www.zamrud.com,网站内容会正常显示。
但是如果我尝试访问 www.berlian.com,它会显示 [=15= 的内容].
我是否缺少 nginx 中的配置?
如果有人遇到同样的问题,我所做的就是关闭 bitnami nginx 容器
docker-compose down
然后再次启动 bitnami nginx 容器
docker-compose up -d
注:
file name for bitnami nginx docker-compose is docker-compose.yaml
I run the docker down and up in the same folder as docker-compose.yaml