Subdomains, Nginx-proxy and Docker-compose

Ivaprag picture Ivaprag · Jul 31, 2017 · Viewed 11.4k times · Source

I'm looking for a way to configure Nginx to access hosted services through a subdomain of my server. Those services and Nginx are instantiated with Docker-compose.

In short, when typing jenkins.192.168.1.2, I should access to Jenkins hosted on 192.168.1.2 redirected with Nginx proxy.

A quick look of what I currently have. It doesn't work without a top domain name, so it works fine on play-with-docker.com, but not locally with for example 192.168.1.2.

server {
    server_name jenkins.REVERSE_PROXY_DOMAIN_NAME;
        location / {
            proxy_pass http://jenkins:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

To have a look of what I want: https://github.com/Ivaprag/devtools-compose

My overall goal is to access remote docker containers without modifying clients' DNS service.

Answer

sharif9876 picture sharif9876 · Jul 31, 2017

Unfortunately nginx doesn't support sub-domains on IP addresses like that.

You would either have to modify the clients hosts file (which you said you didn't want to do)...


Or you can just set your nginx to redirect like so:

location /jenkins {
    proxy_pass http://jenkins:8080;
    ...
}

location /other-container {
    proxy_pass http://other-container:8080;
}

which would allow you to access jenkins at 192.168.1.2/jenkins


Or you can try and serve your different containers through different ports. E.g:

server {
    listen 8081;
    location / {
        proxy_pass http://jenkins:8080;
        ...
    }
}

server {
    listen 8082;
    location / {
        proxy_pass http://other-container:8080;
        ...
    }
}

And then access jenkins from 192.168.1.2:8081/