S
S
Strohmann2018-07-19 14:02:03
Nginx
Strohmann, 2018-07-19 14:02:03

How to configure subdomain permissions in Docker containers?

Hello comrades.
For the first time faced with the NGINX setup.
The task is to redirect requests to subdomains to Docker containers.
There is a container network consisting of a database. NGINX, and the nth number of containers with the application. It is necessary to organize the processing by the server of addresses like anna.mysite.net, bob.mysite.net and the transfer of requests to the appropriate container (anna, bob).
I sketched out such a config.

spoiler
events {
}

http {
  server {
      listen          80;
      server_name     anna.mysite.net www.anna.mysite.net;
      location / {
    proxy_pass  http://anna:3000/;
      }
  }
  server {
      listen          80;
      server_name     bob.mysite.net www.bob.mysite.net;
      location / {
    proxy_pass  http://bob:3000/;
      }
  }
  server {
      listen          80;
      server_name     charlie.mysite.net www.charlie.mysite.net;
      location / {
    proxy_pass  http://charlie:3000/;
      }
  }
}

How to make it universal? Is it possible to use regular expressions to select a subdomain and register it in proxy_pass?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hikmat Abdunabiev, 2018-07-19
@Khikmat

See the official nginx documentation.
There's a section on Names Specified by Regular Expressions .
Here is a piece from there: A
named selection in a regular expression can later be accessed through a variable:

server {
    server_name   ~^(www\.)?(?<domain>.+)$;

    location / {
        root   /sites/$domain;
    }
}

D
Dmitry, 2018-07-19
@q2digger

I recommend looking at the container https://github.com/jwilder/nginx-proxy
This nginx can automatically raise virtual hosts based on the environment variables set when the container was started.
Here is the actual docker-compose.yml of my confluence and postgress sandbox.
variables
VIRTUAL_HOST: 'confluence.local.net'
VIRTUAL_PORT: '8090'
will force nginx-proxy to create the corresponding virt-host with a redirect to the confluence container, port 8080

version: '2'

services:
  confluence:
    image: q2digger/confluence:latest
    container_name: confluence
    hostname: confluence
    volumes:
      - app_data:/var/atlassian/application-data/confluence
    restart: always
    ports:
      - 8090:8090
      - 8091:8091
    networks:
      - confluence
      - proxy
    environment:
      JVM_MINIMUM_MEMORY: '2048m'
      JVM_MAXIMUM_MEMORY: '4096m'
      CATALINA_CONNECTOR_PROXYNAME: 'confluence.local.net'
      CATALINA_CONNECTOR_PROXYPORT: '443'
      CATALINA_CONNECTOR_SCHEME: 'https'
      VIRTUAL_HOST: 'confluence.local.net'
      VIRTUAL_PORT: '8090'
  proxy:
    image: jwilder/nginx-proxy
    ports:
      - 0.0.0.0:80:80
      - 0.0.0.0:443:443
    volumes:
      - ./confluence.local.net.conf:/etc/nginx/vhost.d/confluence.local.net:ro
      - /var/run/docker.sock:/tmp/docker.sock
      - ./certs/:/etc/nginx/certs:ro
    networks:
      - proxy
  database:
    image: atlassian/postgres:9.4
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - confluence
    environment:
      - DB_PASS=atlassian
      - DB_NAME=confluence
      - DB_USER=atlassian

volumes:
  db_data:
  app_data:

networks:
  confluence:
  proxy:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question