Skip to content
Advertisement

Howto debug when nginx gives 502 bad gateway?

On landing.example.com:10000 have I a webserver that works fine, which is a Docker container that exposes port 10000. Its IP is 172.17.0.2.

What I would like is having a nginx reverse proxy on port 80, and send the visitor to different Docker containers depending on the URL they visit.

server {
    listen 80;
    server_name landing.example.com;

    location / {
        proxy_pass http://172.17.0.2:10000/;
    }

    access_log /landing-access.log;
    error_log  /landing-error.log info;
}

When I do this, I get 502 Bad Gateway and the log says

2016/04/14 16:58:16 [error] 413#413: *84 connect()
failed (111: Connection refused) while connecting to upstream, client:
xxx.xxx.xxx.xxx, server: landing.example.com, request: "GET / HTTP/1.1",
upstream: "http://172.17.0.2:10000/", host: "landing.example.com"

Advertisement

Answer

try this:

upstream my_server {
   server 172.17.0.2:10000;
}

server {
   listen 80;
   server_name landing.example.com;
   location / {
      proxy_pass                  http://my_server;
      proxy_set_header            Host $host;
      proxy_set_header            X-Real-IP $remote_addr;
      proxy_http_version          1.1;
      proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header            X-Forwarded-Proto http;
      proxy_redirect              http:// $scheme://;
   }
}

Here you define the upstream server (your server by IP or hostname) and make sure to forward the headers too so the server answering knowns who to answer to.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement