computer_science:docker:docker_nginx_reverse_proxy

This is an old revision of the document!


Nginx as Reverse Proxy for Docker Containers

My setup for the docker env. is the same as described on my DokuWiki Docker Container Portable, even though this should have almost zero effect even if the system is different (yay, docker!).

The materials that I used to learn / troubleshoot / implement this setup:

I will be using a Nginx Server as a Reverse Proxy, so that a) I can host multiple containers which need to be available in the same ports, b) I don't have to access the containers by memorizing ports therefore being able to use carlossousa.tech/wiki or wiki.carlossousa.tech instead of the port and c) easy expandability for new services / containers.

For Nginx as a Reverse Proxy inside Docker we only need 3 basic things:

  1. Mapping of the host ports to the container ports
  2. Mapping a config file to the default Nginx config file - /etc/nginx/nginx.conf
  3. A Nginx config

A sample nginx reverse proxy setup would like something like this:

http {
  server {
    server_name your.server.url;
 
    location /yourService1 {
      proxy_pass http://localhost:80;
      rewrite ^/yourService1(.*)$ $1 break;
    }
 
    location /yourService2 {
      proxy_pass http://localhost:5000;
      rewrite ^/yourService1(.*)$ $1 break;
    }
  }
 
  server {
    server_name another.server.url;
 
    location /yourService1 {
      proxy_pass http://localhost:80;
      rewrite ^/yourService1(.*)$ $1 break;
    }
 
    location /yourService3 {
      proxy_pass http://localhost:5001;
      rewrite ^/yourService1(.*)$ $1 break;
    }
  }
}
version: '3'
services:
  nginx:
    image: nginx:latest
    container_name: production_nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/error.log:/etc/nginx/error_log.log
      - ./nginx/cache/:/etc/nginx/cache
      - /etc/letsencrypt/:/etc/letsencrypt/
    ports:
      - 80:80
      - 443:443
 
  your_app_1:
    image: your_app_1_image:latest
    container_name: your_app_1
    expose:
      - "80"
 
  your_app_2:
    image: your_app_2_image:latest
    container_name: your_app_2
    expose:
      - "80"
 
  your_app_3:
    image: your_app_3_image:latest
    container_name: your_app_3
    expose:
      - "80"

So a sample docker-compose.yml for the Nginx Reverse Proxy using my DokuWiki would be something like:

version: '3'
volumes:
    dokuwiki_data:
        external: true
    dokuwiki_conf:
        external: true
    dokuwiki_lib-plugins:
        external: true
    dokuwiki_lib-tpl:
        external: true
    dokuwiki_logs:
        external: true
 
services:
    nginx:
        image: nginx:latest
        container_name: nginx_reverseproxy
        volumes:
          - ./nginx.conf:/etc/nginx/nginx.conf
          - ./nginx/error.log:/etc/nginx/error_log.log
          - ./nginx/cache/:/etc/nginx/cache
          - /etc/letsencrypt/:/etc/letsencrypt/
        ports:
          - 80:80
          - 443:443
 
    dokuwiki:
        image: 'mprasil/dokuwiki'
        container_name: 'dokuwiki_zebra'
        expose:
          - '80'
        volumes:
            - dokuwiki_data:/dokuwiki/data
            - dokuwiki_conf:/dokuwiki/conf
            - dokuwiki_lib-plugins:/dokuwiki/lib/plugins
            - dokuwiki_lib-tpl:/dokuwiki/lib/tpl
            - dokuwiki_logs:/var/log
  • computer_science/docker/docker_nginx_reverse_proxy.1596101379.txt.gz
  • Last modified: 2023/12/01 12:07
  • (external edit)