我正在尝试使用一个nginx docker-compose文件连接多个docker-compose文件。
例如,此设置很有效(我在运行之前创建了网络):
(请耐心等待。我知道这个例子是多余的,因为我有两个nginx服务器)
#static_site/docker-compose.yml
version: '3'
services:
web:
image: nginx:stable-alpine
container_name: app_web_static
volumes:
- "./nginx.conf:/etc/nginx/conf.d/default.conf"
- "./web:/var/www/static_web"
ports:
- "8080:8080"
networks:
- test_network
#static_site/nginx.conf
server {
listen 8080;
location / {
root /var/www/static_web;
index index.html index.htm;
}
}
#nginx/docker-compose.yml
server {
listen 80;
server_name myapp.local;
#Docker DNS
resolver 127.0.0.11;
location / {
set $docker_host "app_web_static";
proxy_pass http://$docker_host:8080;
}
}
我运行容器,一切都按预期工作。我的网站是通过myapp.local代理的
BUT。当我将静态网站更改为如下的wordpress图像时:
#wordpress/docker-composer.yml
wordpress:
image: wordpress
container_name: app_web
links:
- db:mysql
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_PASSWORD: example
networks:
- test_network
- test_wordpress_db
db:
image: mysql:5.7
container_name: test.web.db
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- ./mysql/data:/var/lib/mysql
networks:
- test_wordpress_db
我再次运行容器(在更新nginx .yml文件之后)。我收到请求记录在我的终端(nginx容器,这意味着请求处理),但作为一个错误:
test.nginx | [error] 5#5: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: myapp.local, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:8080/", host: "myapp.local"
当我从localhost:8080
访问它时,wordpress应用程序正在运行。
此配置中的wordpress app有问题吗?它无法检测到我的主人或什么?我该如何解决这个问题?
答案 0 :(得分:0)
我明白了。
似乎我必须使用内部端口为我的proxy_pass而不是外部端口。例如,对于wordpress docker-compose.yml
文件,我必须像这样编写代理:
#nginx/docker-compose.yml
server {
listen 80;
server_name myapp.local;
#Docker DNS
resolver 127.0.0.11;
location / {
set $docker_host "app_web_static";
proxy_pass http://$docker_host:80; # here. the internal wordpress port
}
}