Skip to content

Instantly share code, notes, and snippets.

@cyanife
Last active July 19, 2018 12:37
Show Gist options
  • Save cyanife/28f7d5f33a7f37f20fc11fac50d58696 to your computer and use it in GitHub Desktop.
Save cyanife/28f7d5f33a7f37f20fc11fac50d58696 to your computer and use it in GitHub Desktop.
[ Using nginx with docker-compose ] #nginx #docker

in nginx.conf:

remind that in a nginx docker container 127.0.0.1 or localhost means the container itself.

nginx container consider all other container as individual hosts in the same network.

when a container's internal port is exposed to a host port, the "host" means docker's physical host operation system, not nginx's container!

so we can't use proxy-pass like "proxy-pass : 127.0.0.1:8080" because nginx docker will recognize "127.0.0.1" as itself.

instead we should use upstream like this:

    upstream myweb {
        server myweb:8080;
    }
    
        server {
        listen 80;

        location /myweb {
            proxy_pass         http://myweb;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment