我想创建两个服务,它们都有自己的nginx。我想使用第三条nginx作为这两条的反向代理,但我得到了
502坏网关
当我请求
http://127.0.0.1:8080/
http://127.0.0.1:8080/one
http://127.0.0.1:8080/two
获得:
http://127.0.0.1:8081
http://127.0.0.1:8082
工作正常
我有这个码头-复合的。
version: "3.3"
services:
nginx-one:
image: nginx:1.17.8
ports:
- "8081:80"
networks:
- frontend
- backend
volumes:
- ./nginx-one/html:/usr/share/nginx/html
nginx-two:
image: nginx:1.17.8
ports:
- "8082:80"
networks:
- frontend
- backend
volumes:
- ./nginx-two/html:/usr/share/nginx/html
nginx-reverse-proxy:
image: nginx:1.17.8
ports:
- "8080:80"
networks:
- frontend
- backend
volumes:
- ./nginx-reverse-proxy/html:/usr/share/nginx/html
- ./nginx-reverse-proxy/conf.d:/etc/nginx/conf.d
debian-network:
image: cslev/debian_networking
stdin_open: true # docker run -i
tty: true # docker run -t
networks:
- frontend
- backend
networks:
frontend:
internal: false
backend:
internal: true
和dir结构
.
├── docker-compose.yml
├── nginx-one
│ └── html
│ └── index.html
├── nginx-reverse-proxy
│ ├── conf.d
│ │ └── default.conf
│ └── html
│ └── index.html
├── nginx-two
└── html
└── index.html
nginx.conf含量
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
和
Con.d/default.conf
server {
listen 80;
location /one {
proxy_pass http://127.0.0.1:8081/;
}
location /two {
proxy_pass http://127.0.0.1:8082/;
}
}
当我评论这行docker-come.yml时
# ./nginx-reverse-proxy/conf.d:/etc/nginx/conf.d
因此不使用conf.d/default.conf
,我从主机的浏览器请求:
http://127.0.0.1:8080/
它给出了来自nginx-反向代理本身的适当响应,但很明显。
http://127.0.0.1:8080/one
http://127.0.0.1:8080/two
不要提供任何回应
http://127.0.0.1:8081
http://127.0.0.1:8082
而不是404。
码头ps输出:
IMAGE COMMAND CREATED STATUS PORTS NAMES
cslev/debian_networking "bash" 25 minutes ago Up 3 minutes nginxproblem_debian-network_1
nginx:1.17.8 "nginx -g 'daemon of…" 47 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp nginxproblem_nginx-reverse-proxy_1
nginx:1.17.8 "nginx -g 'daemon of…" 14 hours ago Up 3 minutes 0.0.0.0:8082->80/tcp nginxproblem_nginx-two_1
nginx:1.17.8 "nginx -g 'daemon of…" 14 hours ago Up 3 minutes 0.0.0.0:8081->80/tcp nginxproblem_nginx-one_1
运行此脚本:
#!/bin/sh
docker exec nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080
docker exec nginxproblem_debian-network_1 curl -sS nginxproblem_nginx-reverse-proxy_1:8080
docker exec nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080/one
docker exec nginxproblem_debian-network_1 curl -sS nginxproblem_nginx-reverse-proxy_1:8080/one
docker exec nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080/two
docker exec nginxproblem_debian-network_1 curl -sS nginxproblem_nginx-reverse-proxy_1:8080/two
docker exec nginxproblem_debian-network_1 curl -sS 127.0.0.1:8081
docker exec nginxproblem_debian-network_1 curl -sS nginxproblem_nginx-one_1:8080
docker exec nginxproblem_debian-network_1 curl -sS 127.0.0.1:8082
docker exec nginxproblem_debian-network_1 curl -sS nginxproblem_nginx-two_1:8082
给予:
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8081: Connection refused
curl: (7) Failed to connect to nginxproblem_nginx-one_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8082: Connection refused
curl: (7) Failed to connect to nginxproblem_nginx-two_1 port 8082: Connection refused
当- ./nginx-reverse-proxy/conf.d:/etc/nginx/conf.d
未被注释时,
与从浏览器访问ip地址相反地注释- ./nginx-reverse-proxy/conf.d:/etc/nginx/conf.d
时的相同输出。
发布于 2021-01-26 13:59:29
除非启用了主机联网,否则127.0.0.1
将指向容器本身。您可以通过服务名称(例如nginx-one
或nginx-two
)从同一网络中的容器中引用其他两个容器。
您还将容器端口80映射到主机上的端口8080/8081/8082。然而,这对同一网络中的容器之间的通信没有任何作用。检查医生们
默认情况下,当使用docker或docker运行创建或运行容器时,它不会将其任何端口发布到外部世界。若要使端口可用于Docker以外的服务或未连接到容器网络的Docker容器,请使用-publish或-p标志。这将创建一个防火墙规则,将容器端口映射到Docker主机上的端口到外部世界。
因此,尝试将http://127.0.0.1:8081/;
更改为http://nginx-one/;
,它应该可以工作。
https://stackoverflow.com/questions/65907904
复制相似问题