nginx docker container: 502 bad gateway response

The Problem

Localhost is a bit tricky when it comes to containers. Within a docker container, localhost points to the container itself. This means, with an upstream like this:

upstream foo{
  server 127.0.0.1:8080;
}

or

upstream foo{
  server 0.0.0.0:8080;
}

you are telling nginx to pass your request to the local host. But in the context of a docker-container, localhost (and the corresponding ip addresses) are pointing to the container itself:

enter image description here

by addressing 127.0.0.1 you will never reach your host machine, if your container is not on the host network.

Solutions

Host Networking

You can choose to run nginx on the same network as your host:

docker run --name nginx -d -v /root/nginx/conf:/etc/nginx/conf.d --net=host nginx

Note that you do not need to expose any ports in this case.

This works though you lose the benefit of docker networking. If you have multiple containers that should communicate through the docker network, this approach can be a problem. If you just want to deploy nginx with docker and do not want to use any advanced docker network features, this approach is fine.

Access the hosts remote IP Address

Another approach is to reconfigure your nginx upstream directive to directly connect to your host machine by adding its remote IP address:

upstream foo{
  //insert your hosts ip here
  server 192.168.99.100:8080;
}

The container will now go through the network stack and resolve your host correctly:

enter image description here

You can also use your DNS name if you have one. Make sure docker knows about your DNS server.


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

server {
   listen            80;
   server_name  localhost;
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;
}

Just to complete other answers, I'm using mac for development and using host.docker.internal directly on upstream worked for me and no need to pass the host remote IP address. Here is config of the proxy nginx:

events { worker_connections 1024; }

http {
    upstream app1 {
        server host.docker.internal:81;
    }

    upstream app1 {
        server host.docker.internal:82;
    }
    
    server {
        listen 80;
        server_name app1.com;
    
        location / {
          proxy_pass    http://app1;
        }
    }

    server {
        listen 80;
        server_name app2.com;
    
        location / {
          proxy_pass    http://app2;
        }
    }
}

As you can see, I used different ports for different apps behind the nginx proxy. I used port 81 for the app1 and port 82 for the app2 and both app1 and app2 have their own nginx containers:

For app1:

docker run --name nginx -d -p 81:80 nginx

For app2:

docker run --name nginx -d -p 82:80 nginx

Also, please refer to this link for more details: docker doc for mac


What you can do is configure proxy_pass that from container perspective the adress will be pointing to your real host.

To get host address from container perspective you can do as following on Windows with docker 18.03 (or more recent):

Run bash on container from host where image name is nginx (works on Alpine Linux distribution):

 docker run -it nginx /bin/ash

Then run inside container

/ # nslookup host.docker.internal

Name:      host.docker.internal
Address 1: 192.168.65.2

192.168.65.2 is the host's IP - not the bridge IP like in spinus accepted answer.

I am using here host.docker.internal:

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Windows.

Then you can change nginx config to: proxy_pass http://192.168.65.2:{your_app_port};

and it should work fine.

Remember to provide the same port as your local application runs with.


# the upstream component nginx needs to connect to
upstream django {

    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket

    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}


location / {

        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }

complete reference: https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html