Docker - 从另一个容器使用 nginx server_name 访问 nginx 容器
Docker - Access nginx container using nginx server_name from another container
我有一个 node.js 应用程序 (web-app) 和两个 lumen 应用程序 (api, customer-api),它们由侦听端口 80 的 nginx 容器进行负载平衡.
我的docker-compose.yml文件:
version: '2'
services:
nginx:
build:
context: ../
dockerfile: posbytz-docker/nginx/dockerfile
volumes:
- api
- customer-api
ports:
- "80:80"
networks:
- network
depends_on:
- web-app
- api
- customer-api
web-app:
build:
context: ../
dockerfile: posbytz-docker/web-app-dockerfile
volumes:
- ../web-app:/posbytz/web-app
- /posbytz/web-app/node_modules
ports:
- "3004:3004"
networks:
- network
api:
build:
context: ../
dockerfile: posbytz-docker/api-dockerfile
volumes:
- ../api:/var/www/api
networks:
- network
customer-api:
build:
context: ../
dockerfile: posbytz-docker/customer-api-dockerfile
volumes:
- ../customer-api:/var/www/customer-api
networks:
- network
redis:
image: redis
ports:
- "6379:6379"
networks:
- network
memcached:
image: memcached
ports:
- "11211:11211"
networks:
- network
mysql:
image: mysql:5.7
volumes:
- ./db-data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- network
adminer:
image: adminer
restart: always
ports:
- "9001:8080"
networks:
- network
networks:
network:
driver: bridge
由于我使用的是桥接网络,因此我能够使用容器名称从另一个容器访问每个容器。但我想要的是,使用其 nginx 配置的 server_name 访问容器。
下面是各个应用的nginx配置,
网络-app.conf:
server {
listen 80;
server_name posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
proxy_pass http://web-app:3004;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
api.conf:
server {
listen 80;
index index.php index.html;
root /var/www/api/public;
server_name api.posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
客户-api.conf
server {
listen 80;
index index.php index.html;
root /var/www/customer-api/public;
server_name customer-api.posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass customer-api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
问题
我想从 web-app 容器访问 api 和 customer-api 容器。问题是当我尝试 curl http://nginx
时,我只收到来自 api 容器的响应。有没有办法通过nginx容器访问customer-api容器?
我试过的
当我在 web-app 容器的 /etc/hosts 文件中手动映射 nginx 容器 (172.21.0.9) 的 IP 及其各自的 server_name 时,它似乎可以工作。
我在网络应用程序容器的 /etc/hosts 文件中添加的内容:
172.21.0.9 api.posbytz.local
172.21.0.9 customer-api.posbytz.local
有没有其他方法可以在没有人工干预的情况下实现这一目标?
最终通过更改 customer-api.conf 上的 nginx 配置以侦听端口 81 使其工作。 listen 80;
到 listen 81;
。现在 http://nginx
解析为 http://api:9000
,http://nginx:81
解析为 http://customer-api:9000
您可以使用别名:
networks:
some-network:
aliases:
- api.posbytz.local
- customer-api.posbytz.local
我有一个 node.js 应用程序 (web-app) 和两个 lumen 应用程序 (api, customer-api),它们由侦听端口 80 的 nginx 容器进行负载平衡.
我的docker-compose.yml文件:
version: '2'
services:
nginx:
build:
context: ../
dockerfile: posbytz-docker/nginx/dockerfile
volumes:
- api
- customer-api
ports:
- "80:80"
networks:
- network
depends_on:
- web-app
- api
- customer-api
web-app:
build:
context: ../
dockerfile: posbytz-docker/web-app-dockerfile
volumes:
- ../web-app:/posbytz/web-app
- /posbytz/web-app/node_modules
ports:
- "3004:3004"
networks:
- network
api:
build:
context: ../
dockerfile: posbytz-docker/api-dockerfile
volumes:
- ../api:/var/www/api
networks:
- network
customer-api:
build:
context: ../
dockerfile: posbytz-docker/customer-api-dockerfile
volumes:
- ../customer-api:/var/www/customer-api
networks:
- network
redis:
image: redis
ports:
- "6379:6379"
networks:
- network
memcached:
image: memcached
ports:
- "11211:11211"
networks:
- network
mysql:
image: mysql:5.7
volumes:
- ./db-data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- network
adminer:
image: adminer
restart: always
ports:
- "9001:8080"
networks:
- network
networks:
network:
driver: bridge
由于我使用的是桥接网络,因此我能够使用容器名称从另一个容器访问每个容器。但我想要的是,使用其 nginx 配置的 server_name 访问容器。
下面是各个应用的nginx配置,
网络-app.conf:
server {
listen 80;
server_name posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
proxy_pass http://web-app:3004;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
api.conf:
server {
listen 80;
index index.php index.html;
root /var/www/api/public;
server_name api.posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
客户-api.conf
server {
listen 80;
index index.php index.html;
root /var/www/customer-api/public;
server_name customer-api.posbytz.local;
resolver 127.0.0.11 valid=10s;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass customer-api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
问题
我想从 web-app 容器访问 api 和 customer-api 容器。问题是当我尝试 curl http://nginx
时,我只收到来自 api 容器的响应。有没有办法通过nginx容器访问customer-api容器?
我试过的
当我在 web-app 容器的 /etc/hosts 文件中手动映射 nginx 容器 (172.21.0.9) 的 IP 及其各自的 server_name 时,它似乎可以工作。
我在网络应用程序容器的 /etc/hosts 文件中添加的内容:
172.21.0.9 api.posbytz.local
172.21.0.9 customer-api.posbytz.local
有没有其他方法可以在没有人工干预的情况下实现这一目标?
最终通过更改 customer-api.conf 上的 nginx 配置以侦听端口 81 使其工作。 listen 80;
到 listen 81;
。现在 http://nginx
解析为 http://api:9000
,http://nginx:81
解析为 http://customer-api:9000
您可以使用别名:
networks:
some-network:
aliases:
- api.posbytz.local
- customer-api.posbytz.local