nginx docker container: 502 bad gateway response

nginx docker container: 502 bad gateway response

b’

The Problem

n

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

n

upstream foo{n  server 127.0.0.1:8080;n}n

n

or

n

upstream foo{n  server 0.0.0.0:8080;n}n

n

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

n

enter

n

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

n

Solutions

n

Host Networking

n

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

n

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

n

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

n

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.

n

Access the hosts remote IP Address

n

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

n

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

n

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

n

enter

n

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;

n

server {n   listen            80;n   server_name  localhost;nlocation / {n   proxy_set_header X-Real-IP $remote_addr;n   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;n   proxy_set_header X-Forwarded-Proto $scheme;n   proxy_set_header Host $http_host;n   proxy_set_header X-NginX-Proxy true;nn   proxy_redirect off;n   proxy_pass http://myserver;n}n

nginx docker container: 502 bad gateway response

Just to complete other answers, Im 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:

n

events { worker_connections 1024; }nnhttp {n    upstream app1 {n        server host.docker.internal:81;n    }nn    upstream app1 {n        server host.docker.internal:82;n    }n    n    server {n        listen 80;n        server_name app1.com;n    n        location / {n          proxy_pass    http://app1;n        }n    }nn    server {n        listen 80;n        server_name app2.com;n    n        location / {n          proxy_pass    http://app2;n        }n    }n}n

n

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:

n

For app1:

n

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

n

For app2:

n

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

n

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

Leave a Reply

Your email address will not be published.