Subdomains, Nginx-proxy and Docker-compose

I am looking for a way to configure Nginx to access hosted services through a subdomain of my server. These services and Nginx are created using Docker-compose.

In short, when I enter jenkins.192.168.1.2 I have to access Jenkins, hosted on 192.168.1.2 , redirected from a Nginx proxy.

A quick look at what I have. It does not work without the upper domain name, so it works fine on play-with-docker.com , but not locally, for example, with 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 see what I want: https://github.com/Ivaprag/devtools-compose

My common goal is to access remote docker containers without changing the DNS client service.

+8
source share
3 answers

Unfortunately, nginx does not support subdomains on such IP addresses.

You will need to either modify the client hosts file (which, as you said, you do not want to do) ...


Or you can simply configure nginx to redirect like this:

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

which will allow you to access jenkins in 192.168.1.2/jenkins


Or you can try and serve different containers through different ports. For instance:

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

And then login to jenkins from 192.168.1.2:8081/

+6
source

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

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

This allows you to add an unlimited number of web service containers to the backend of a specific 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 server mounts the host docker socket file to get information about other containers running, if any of them has the env VIRTUAL_HOST variable set, it will add it to its configuration.

+3
source

I tried to set up subdomains in nginx (host) for two virtual hosts in one LXC container.

How it works for me:

For apache (in the container), I created two virtual hosts: one in port 80, and the other in port 90.
To enable port 90 in apache2 (container), you had to add the line “Listen 90” below “Listen 80” to /etc/apache2/ports.conf

Two DOMAINS domains are configured for NGINX (the host computer), both on port 80, creating independent .conf files in / etc / nginx / sites-available. A symbolic link has been created for each file to / etc / nginx / sites-enabled.

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

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

It was for me!

+1
source

Source: https://habr.com/ru/post/1270392/


All Articles