14

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.

Ivaprag
  • 591
  • 1
  • 5
  • 9

3 Answers3

10

If you are already using docker-compose I recommend using the jwilder nginx-proxy container.

https://github.com/jwilder/nginx-proxy

This allows you to add unlimited number of web service containers to the backend of the defined nginx proxy, for example:

nginx-proxy:
  image: jwilder/nginx-proxy
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - "/etc/nginx/vhost.d"
    - "/usr/share/nginx/html"
    - "/var/run/docker.sock:/tmp/docker.sock:ro"
    - "nginx_certs:/etc/nginx/certs:rw"
nginx:
  build:
   context: ./docker/nginx/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host1.com
nginx_2:
  build:
   context: ./docker/nginx_2/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host2.com
apache_1:
  build:
   context: ./docker/apache_1/
   dockerfile: Dockerfile
  volumes_from:
     - data
  environment:
     VIRTUAL_HOST: www.host3.com

The nginx-proxy mount the host docker sock file in order to get information about the other containers running, if any of them have the env variable VIRTUAL_HOST set then it will add it to its configuration.

Marcos Pagnucco
  • 351
  • 1
  • 5
9

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/

sharif9876
  • 680
  • 6
  • 19
  • Thank you, I've tried your suggestion but it doesn't work, I have a 404 error when connecting to 192.168.1.2/jenkins... Guess I'll have to modify the hosts file – Ivaprag Jul 31 '17 at 14:01
  • The error is finally the overridden address, I've got 192.168.1.2/login?from=%2Fjenkins when going to 192.162.1.2/jenkins. I'm gonna try more things – Ivaprag Jul 31 '17 at 14:09
  • 1
    Yeah looks like jenkins is redirecting stuff.. I've edited the answer with a different possible solution. Let me know if that helps – sharif9876 Jul 31 '17 at 14:39
  • Using different port for each service works! I remember I've tried this solution but with ports 80 to 90, I didn't worked. Many thanks mate! – Ivaprag Jul 31 '17 at 14:53
  • Could you please give an example of how this would hook into docker-compose as in the title? – Jack Sep 04 '18 at 11:21
0

I was trying to configure subdomains in nginx (host), for two virtualhosts in one LXC container.

The way it worked for me:

For apache (in the container), I created two virtual hosts: one in port 80 and the other one in port 90.
For enabling port 90 in apache2 (container), it was necessary to add the line "Listen 90" below "Listen 80" in /etc/apache2/ports.conf

For NGINX (host machine), configured two DOMAINS, both in port 80 creating independent .conf files in /etc/nginx/sites-available. Created symbolic link for each file to /etc/nginx/sites-enabled.

In the first NGINX myfirstdomain.conf file, redirect to http://my.contai.ner.ip:80.

In the second NGINX myseconddomain.conf file, redirect to http://my.contai.ner.ip:90

That was it for me !