title | description |
---|---|
NGINX Configuration Guide |
Configure an NGINX web proxy for your backend service with SSL support |
Our Node.js template includes a minimal NGINX configuration that works out-of-the-box for most deployments, but you can expand it as needed.
The default template does not include SSL support. Below are detailed instructions on setting up SSL with Let's Encrypt.
SSL certificates are generated on the host and then mounted into the NGINX Docker container.
- Install Certbot (Let's Encrypt client) on your host if it’s not already installed:
sudo apt install certbot
- Generate SSL certificates for your domain. Replace
yourdomain.com
with your actual domain name:
sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com
Certificates will be stored in /etc/letsencrypt/live/yourdomain.com/
.
Modify your nginx.conf
to enable SSL:
- Add an HTTPS server block in
nginx.conf
, configured as follows:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/ssl/certs/fullchain.pem;
ssl_certificate_key /etc/ssl/certs/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://backend:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- Add an HTTP to HTTPS redirect block (optional):
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
Ensure SSL certificates are available to the NGINX container by mounting them in docker-compose.yml
.
Replace yourdomain.com
with your domain name:
services:
nginx:
volumes:
- ./etc/letsencrypt/live/yourdomain.com:/etc/ssl/certs:ro
- Open your crontab to add an automated renewal job:
sudo crontab -e
- Add the renewal command to run daily at 3 AM (or adjust the schedule as desired):
0 3 * * * certbot renew --quiet && docker compose -f /path-to-your-project/docker-compose.yml restart nginx
This setup will check for certificate renewal daily and restart NGINX if renewal occurs.