
Nginx as a reverse proxy with HTTPS and Certbot
DevOpsGuidesNginxHTTPSCertbotLinux
Publish an application that listens locally behind Nginx with Let's Encrypt HTTPS, and the real problems I ran into: certificates, renewal hooks and the default server.
Contents
A reverse proxy receives requests from the internet and forwards them to an application that only listens locally. This way the application is not directly exposed and Nginx handles HTTPS. I use it in my homelab and in Unit 4 of my Web Application Deployment course; this guide covers the basic path and, at the end, the mistakes I made along the way.
When everything is set up, the app answers over HTTPS:
curl -I https://app.example.com
# HTTP/2 200
The layout is always the same: the browser talks to Nginx on port 443 and Nginx talks to the application on a local port.
Requirements
- An Ubuntu server with Nginx (
sudo apt install -y nginx). - A domain with a DNS record pointing to the server (for example
app.example.com). - An application listening on
127.0.0.1:3000. - Ports 80 and 443 reachable from the internet. If they are not, see the DNS certificate section.
Make the application listen only on 127.0.0.1, not on 0.0.0.0. That way nobody reaches it without going through Nginx. It matters: many frameworks trust the X-Forwarded-* headers, and if the application were reachable from outside, any client could forge them.
Configure the site
Create /etc/nginx/sites-available/app.example.com:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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;
proxy_set_header X-Forwarded-Host $host;
}
}
Each header tells the application what Nginx saw: Host is the name the client asked for (without it the application would see 127.0.0.1), X-Forwarded-For is the chain of IPs the request went through, and X-Forwarded-Proto says whether the client used http or https.
Enable it and reload. The golden rule is to always validate before reloading:
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Test a site without touching DNS
Before the domain points at the server you can test the site by forcing the Host header:
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example.com' http://127.0.0.1/
Certificate with Certbot
If the server is reachable from the internet on port 80, the short path is the Nginx plugin:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com
Certbot edits the configuration to add the HTTPS block and the redirect from HTTP.
When port 80 is not reachable: DNS validation
At home my router forwards ports 80 and 443 to another machine, so Let’s Encrypt never reached my server and HTTP validation failed. The fix is DNS validation (DNS-01): Certbot creates a temporary TXT record at your DNS provider and needs no open port. With Cloudflare:
sudo apt install -y certbot python3-certbot-dns-cloudflare
sudo mkdir -p /etc/letsencrypt/cloudflare
sudo nano /etc/letsencrypt/cloudflare/cloudflare.ini # dns_cloudflare_api_token = YOUR_TOKEN
sudo chmod 600 /etc/letsencrypt/cloudflare/cloudflare.ini
sudo certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare/cloudflare.ini \
--dns-cloudflare-propagation-seconds 30 \
-d app.example.com
The Cloudflare token only needs the Zone:DNS:Edit permission on the zone. With certonly Certbot does not touch Nginx, so you write the HTTPS block yourself:
server {
listen 80;
server_name app.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
To enable HTTP/2 there is a version trap: before Nginx 1.25.1 you write listen 443 ssl http2;, and the http2 on; directive only exists from 1.25.1. Ubuntu 24.04 ships 1.24.0, so there http2 on; gives unknown directive.
Automatic renewal
The package installs a systemd timer that tries to renew twice a day. To see which certificates you have and when they expire, without changing anything:
systemctl list-timers certbot.timer
sudo certbot certificates
Nginx reads the certificate at startup, so after a renewal it has to be reloaded. A deploy-hook runs only when a certificate was actually renewed:
sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh >/dev/null <<'HOOK'
#!/bin/sh
systemctl reload nginx
HOOK
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
A warning I learned the hard way: certbot renew --dry-run also runs the hooks. I had a script in renewal-hooks/post/ that recreated a container, and post hooks run after any renewal, even a simulation and even for a certificate you do not care about. Since that container is my VPN, testing the renewal takes it down. If a hook is expensive or disruptive, use deploy instead of post, or add a guard at the top:
case "${RENEWED_LINEAGE:-}" in */app.example.com) ;; *) exit 0 ;; esac
The default server on port 80
If none of your server blocks declares default_server, Nginx uses the first one it reads for that port as the default, and the files in sites-enabled are read in alphabetical order. Result: any request with an unknown Host ends up in a real application. It happened to me with the login page of a service whose DNS record no longer existed.
The fix is to declare a default server that drops the connection:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
To check it, make up a Host:
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: made-up.test' http://127.0.0.1/
When the upstream speaks HTTPS
If the application behind Nginx already terminates its own TLS, proxy_pass points to https:// and you need one more directive; without it the connection fails because Nginx does not send the server name in the handshake:
proxy_pass https://127.0.0.1:9236;
proxy_ssl_server_name on;
Common problems
- 502 Bad Gateway: the application is not listening on the port in
proxy_pass. Check withss -tlnp | grep 3000. - Certbot fails to validate: DNS does not point to the server yet, or port 80 does not reach it. If you cannot open it, use DNS validation.
nginx -treportsunknown directive "http2": your Nginx is older than 1.25.1. Uselisten 443 ssl http2;.- A made-up hostname shows you an application: you are missing a default server.
- After renewing, the browser still sees the old certificate: Nginx was not reloaded. Check the renewal hook.
Copyright: CC BY-NC-ND 4.0 · José Ángel Alejo Sillero