A
A
AE422020-06-18 09:40:33
Nginx
AE42, 2020-06-18 09:40:33

Multiple nginx configs on docker?

Good morning!

I have several projects that are on 1 server
How can I configure nginx in docker-compose so that it uses several configs

At the moment my docker-compose.yml looks like this:

version: '3.6'
services:
  postgres:
    image: postgres:12
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: qwe
      POSTGRES_PASSWORD: qwe
      POSTGRES_DB: qwe
  graphql-engine:
    image: hasura/graphql-engine:v1.2.2
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgres://qwe:[email protected]:5432/qwe
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      ## uncomment next line to set an admin secret
      HASURA_GRAPHQL_ADMIN_SECRET: admin
  backend:
    build: ./backend/
    container_name: backend
    restart: always
    ports:
      - '4000:4000'
    env_file:
      - ./backend/.env
  nginx:
    restart: always
    image: nginx:stable
    container_name: hasura-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /root/certs/:/root/certs/
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - graphql-engine
volumes:
  db_data:

/nginx/default.conf
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name hasura.test.ru;

  ssl_certificate /root/certs/cert.crt;
  ssl_certificate_key /root/certs/cert.key;

  location / {
    proxy_pass http://graphql-engine:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name api.test.ru;

  ssl_certificate /root/certs/cert.crt;
  ssl_certificate_key /root/certs/cert.key;

  location / {
    proxy_pass http://backend:4000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}


How to make graphql-engine and backend have their own docker-compose.yml?
At the moment, this method works, but in order to update any service, you have to rebuild all these components

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Robur, 2020-06-18
@Robur

here, not "for example" is needed, but configs, a description of containers, and what listens where and where it proxies.
In general, look at dokku - there is nginx and built-in proxying, just say which container to bind to which host / port.

C
Constantine Karnaukhov, 2020-06-19
@genteelknight

During development, you can take a proxy that generates an nginx config based on running containers. For example, in my work I use this https://github.com/spaceonfire/localhost-docker
On the server, of course, it is better to take some kind of kubernetes or the previously mentioned dokku

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question