Docker nginx reverse proxy gives "502 Bad Gateway"

74,725

Solution 1

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?

Solution 2

I had the same "502 Bad Gateway" error, but the solution was to tune proxy_buffer_size following this post instructions:

proxy_buffering off;
proxy_buffer_size 16k;
proxy_busy_buffers_size 24k;
proxy_buffers 64 4k;

Solution 3

I had the same error, but for a web application that was just not serving at the IP and port mentioned in the config.

So say you have this:

location /game {
    proxy_pass "http://game:9999";
}

Then make sure the web application that you expect at http://game:9999 is really serving from within a docker container named 'game' and the code is set to serve the app at port 9999.

Solution 4

For me helped this line of code proxy_set_header Host $http_host;

location / {
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header Host $http_host;
   proxy_set_header X-NginX-Proxy true;

   proxy_redirect off;
   proxy_pass http://myserver;
}
Share:
74,725
jollege
Author by

jollege

awesomemap.tools - expert on maps, GIS and geospatial software - consulting and development | blockchain enthusiast

Updated on July 09, 2022

Comments

  • jollege
    jollege almost 2 years

    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
    
  • samprog
    samprog over 6 years
    Stackoverflow is great for rubber duck debugging :)
  • jollege
    jollege over 6 years
    Indeed! I hadn't heard that term before - but man, do I know the concept!
  • Alex K
    Alex K over 6 years
    "Remember to use docker port, not host port." Saved me. Thanks.
  • Roel
    Roel over 3 years
    How to do this with a docker image?
  • funder7
    funder7 over 3 years
    lol guys...that valid for me too, I know what's rubber duck debugging :-D
  • Genotypek
    Genotypek over 2 years
    That was exactly what fixed all my problems with reverse proxy nginx! Thanks a lot!
  • Jamviet.com
    Jamviet.com about 2 years
    You save my day! Thanks so much!