This is an all-in-one docker compose file to setup a nginx proxy server with TLS using certbot. This configuration tries to renew the certificates and reloads the nginx configuration every 24 hours. You just have to create a .env file next to the compose file with the server name and an email for the ACME protocol, like this:
SERVER_NAME=...
EMAIL=...There's an external docker network called nginx-network in the compose configuration, that you can use to have access to other services running in the same network. You have to create it before running docker compose up:
docker network create nginx-network
There's a nginx server listening in port 80 that serves the ACME files and redirects any requests to the https server. You can extend this server through ./user_conf/http.conf.
There's also an nginx server listening in port 443 with all the configuration to use TLS. You can extend this server through ./user_conf/https.conf.
You can also add more servers by adding .conf files to ./user_conf/conf.d/.
You can use files from the host in the server by adding them to a folder next to the compose file called shared. This folder is in sync through a docker volume located in /nginx/shared inside the container, and you can use it to serve staticfiles or store uploaded files.
For example, you can have a ./user_conf/https.conf file like the following to serve any files in the shared folder:
location / {
root /nginx/shared;
try_files $uri $uri/ =404;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host my.site.com;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
}
This way you can have a ./user_conf/https.conf file like the following to proxy all the requests to a specific service:
location / {
proxy_pass http://<SERVICE_NAME>:<PORT>;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Url-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host my.site.com;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
}
To start the server you can run:
docker compose up -d