在配置多个服务(其中Nginx是代理服务器)时,我有些困惑。
运行:
docker -v
Docker version 19.03.8, build afacb8b7f0
docker-compose -v
docker-compose version 1.23.2, build 1110ad01
我想从此测试开始,所有内容都在同一docker-compose.yml-file中: 链接到jwilder/nginx
在开始之前更新/ etc / hosts
这是我的 docker-compose.yml文件(obs->版本3.7)
version: '3.7'
services:
proxy:
image: jwilder/nginx-proxy:0.7.0
container_name: proxy-test
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx-proxy.conf:/etc/nginx/conf.d/nginx-proxy.conf:ro
container1:
image: httpd:2.4
container_name: container-1
environment:
- VIRTUAL_HOST:container1.com
ports:
- 8080:80
container2:
image: httpd:2.4
container_name: container-2
environment:
- VIRTUAL_HOST:container2.com
ports:
- 8081:80
这是我的 nginx-proxy.conf :
server {
listen 80;
server_name container1.com;
location / {
proxy_pass http://localhost:8080;
}
}
server {
listen 80;
server_name container2.com;
location / {
proxy_pass http://localhost:8081;
}
}
此后,我运行
docker exec container-1 sed -i 's/It works!/Container 1/' /usr/local/apache2/htdocs/index.html
AND
docker exec container-2 sed -i 's/It works!/Container 2/' /usr/local/apache2/htdocs/index.html
测试1:弯曲端口8080和8081
curl localhost:8080
response -> Container 1
curl localhost:8081
response -> Container 2
测试2:卷曲到container1.com AND container2.com
curl container1.com
status 502
curl container2.com
status 502
我的conf中的设置是否错误?
故障排除1:
docker exec -it proxy-test bash
我可以看到nginx-proxy.conf位于目录(/etc/nginx/conf.d)
/etc/nginx/conf.d/default.conf也在那里
故障排除2:代理日志(Connection refused - while connecting to upstream
)
proxy-test | nginx.1 | 2020/04/03 10:52:08 [error] 61#61: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.29.0.1, server: container1.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "container1.com"
proxy-test | nginx.1 | 2020/04/03 10:52:08 [error] 61#61: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 172.29.0.1, server: container1.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "container1.com"
答案 0 :(得分:0)
找到2个解决方案。
(1)
第一种是使用容器名称更新nginx-proxy.conf,而不是指向http://localhost:8080;
和http://localhost:8081;
:
新的配置文件
server {
listen 80;
server_name container1.com;
location / {
proxy_pass http://container-1;
}
}
server {
listen 80;
server_name container2.com;
location / {
proxy_pass http://container-2;
}
}
(2) 省略nginx-proxy.conf-file,docker-compose.yml可以正确映射内容。