Set up Nginx reverse proxy HTTP/HTTPS with Docker Compose
Installing Nginx reverse proxy HTTP/HTTPS
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:
- Basic networking
- Docker / Docker Compose
- Nginx
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:
- Docker Compose for Traefik (reverse proxy HTTP)
- Set up Nginx reverse proxy HTTP/HTTPS with Docker Compose
- Docker compose for VSCode server
- Set up Pytorch Env with Docker within 1 Minute
- Docker Compose for Jekyll
- Docker Compose for Odoo 10
- Docker Compose for Odoo 11
- Docker Compose for Odoo 12
- Docker Compose for Odoo 13