nginx docker container: 502 bad gateway response nginx docker container: 502 bad gateway response nginx nginx

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