I'm trying to have a docker container with nginx work as reverse proxy to other docker containers and I keep getting "Bad Gateway" on locations other other than the base location '/'.
I have the following server block:
server { listen 80; location / { proxy_pass "http://game2048:8080"; } location /game { proxy_pass "http://game:9999"; } }
It works for http://localhost
but not for http://localhost/game
which gives "Bad Gateway" in the browser and this on the nginx container:
[error] 7#7: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET /game HTTP/1.1", upstream: "http://172.17.0.4:9999/game", host: "localhost"
I use the official nginx docker image and put my own configuration on it. You can test it and see all details here: https://github.com/jollege/ngprox1
Any ideas what goes wrong?
NB: I have set local hostname entries on docker host to match those names:
127.0.1.1 game2048 127.0.1.1 game
In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn't able to get a valid or any response from the upstream server. If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy.
In order to get the reverse proxy to actually work, we need to reload the nginx service inside the container. From the host, run docker exec <container-name> nginx -t . This will run a syntax checker against your configuration files. This should output that the syntax is ok.
I fixed it! I set the server name in different server blocks in nginx config. Remember to use docker port, not host port.
server { listen 80; server_name game2048; location / { proxy_pass "http://game2048:8080"; } } server { listen 80; server_name game; location / { # Remember to refer to docker port, not host port # which is 9999 in this case: proxy_pass "http://game:8080"; } }
The github repo has been updated to reflect the fix, the old readme file is there under ./README.old01.md
.
Typical that I find the answer when I carefully phrase the question to others. Do you know that feeling?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With