im very new with this docker thing, im trying create a load balancer of 2 server, the first port is localhost:2222 and localhost:3333, it can be accessed properly.
http {
upstream all {
server 127.0.0.1:3333;
server 127.0.0.1:2222;
}
server{
listen 8080;
location / {
proxy_ssl_server_name on;
proxy_pass http://all/;
}
}
}
events {}
but when i try integrate it with nginx load balancer with this configuration. with localhost:8080 as listener, it cannot be accessed it always displayed 502 bad gateway and the nginx shown this error
2021/12/04 10:04:23 [error] 32#32: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:2222/", host: "localhost:8080"
2021/12/04 10:04:23 [error] 32#32: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3333/", host: "localhost:8080" 172.17.0.1 - - [04/Dec/2021:10:04:23 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0"
2021/12/04 10:04:27 [error] 32#32: *4 no live upstreams while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://all/", host: "localhost:8080" 172.17.0.1 - - [04/Dec/2021:10:04:27 +0000] "GET / HTTP/1.1" 502 157 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0"
anybody know how to solve it? i have been struggling this for a long time
Since your Nginx is running in a container its localhost isn't the same as the host machine's. Every container has its own localhost.
Probably the best way would be to create a new network and put the containers in it and use names to connect to the other containers. Name resolution works in a self-created network and you don't need to know the IP addresses or expose any ports to the host.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.