Set up Nginx reverse proxy HTTP/HTTPS with Docker Compose

  Apr 2, 2020      2m      0   
 

Installing Nginx reverse proxy HTTP/HTTPS

Set up Nginx reverse proxy HTTP/HTTPS with Docker Compose

Nginx reverse proxy with Docker Compose

Architecture:

  • Sub domain: dev.minhng.info
  • Backend or web service running @ port 8445 (http://0.0.0.0:8445/)

Target:

Open http://dev.minhng.info or https://dev.minhng.info to access the service running at http://0.0.0.0:8445/ on the remote machine (server).

Mapping: dev.minhng.info -> http://0.0.0.0:8445/

We will use docker-compose for easy/quick setup this architecture.

Required knowledge:

Nginx reverse proxy HTTP

Mapping: http://dev.minhng.info -> http://0.0.0.0:8445/

web.conf

server {
    listen 80;
    server_name dev.minhng.info;

    sendfile on;

    charset utf-8;
    # max upload size
    client_max_body_size 50G; # adjust to taste

    location / {
        proxy_pass http://0.0.0.0:8445/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

docker-compose.yml

version: '3.3'
services:
    nginx:
        image: nginx:stable-alpine
        volumes:
            - ./web.conf:/etc/nginx/conf.d/default.conf
        network_mode: host
        ipc: host
        restart: unless-stopped     # or "always"
        command: /bin/sh -c "nginx -g 'daemon off;'"        

Put these two files (web.conf and docker-compose.yml) in the same folder and run the following command to set up:

$ docker-compose up -d

Restart nginx:

$ docker-compose restart

Note: modify web.conf to fit your requirement.

Nginx reverse proxy HTTPS (secure)

Mapping: https://dev.minhng.info -> http://0.0.0.0:8445/

web.conf

server {
    listen 80;
    listen [::]:80;
    server_name dev.minhng.info;
    return         301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name dev.minhng.info;
    ssl_certificate /root/ssl/official/gveyes_net.crt;
    ssl_certificate_key /root/ssl/official/gveyes.net.private_key;

    sendfile on;

    charset utf-8;
    # max upload size
    client_max_body_size 50G; # adjust to taste

    location / {
        proxy_pass http://0.0.0.0:8445/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

docker-compose.yml

version: '3.3'
services:
    nginx:
        image: nginx:stable-alpine
        volumes:
            - ./web.conf:/etc/nginx/conf.d/default.conf
            - /root/ssl/official/gveyes_net.crt:/root/ssl/official/gveyes_net.crt
            - /root/ssl/official/gveyes.net.private_key:/root/ssl/official/gveyes.net.private_key
        network_mode: host
        ipc: host
        restart: unless-stopped     # or "always"
        command: /bin/sh -c "nginx -g 'daemon off;'"        

Put these two files (web.conf and docker-compose.yml) in the same folder and run the following command to set up:

$ docker-compose up -d

Restart nginx:

$ docker-compose restart

Note: modify web.conf to fit your requirement.

Sample docker-compose.yml for nginx, mounted volumes:

  • Nginx config @ web.conf file
  • Static files in html folder
  • SSL certificates in ssl folder
  • Logging in log folder
version: '3.3'
services:
    nginx:
        image: nginx:stable-alpine
        volumes:
            - ./web.conf:/etc/nginx/conf.d/default.conf
            - ./html/:/usr/share/nginx/html/
            - ./ssl/:/root/ssl/official/
            - ./log/:/var/log/nginx/
        network_mode: host
        ipc: host
        restart: unless-stopped     # or "always"
        command: /bin/sh -c "nginx -g 'daemon off;'"

References:

  • [https://hub.docker.com//nginx/](https://hub.docker.com//nginx/)

More docker-compose files for you:



Khám phá xử lý ảnh - GVGroup




-->