Reverse Proxy & TLS
Ninety percent of what nginx does in my life is accept HTTPS on 443 and hand it to some app on 127.0.0.1:8080 that was never meant to face the internet. That’s fine. That’s the job.
Minimal reverse proxy
Section titled “Minimal reverse proxy”server { listen 80; server_name app.example.com;
location / { proxy_pass http://127.0.0.1:8080;
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; }}Without those headers, your app thinks every request comes from 127.0.0.1 over plain HTTP. Then it generates http:// redirect URLs, logs the wrong IPs, and rate-limits the proxy itself. Put them in a snippet and include it everywhere.
Use 127.0.0.1, not localhost. localhost might resolve to ::1 first, and if the app only listens on IPv4, you get intermittent 502s and a deep sense of betrayal.
The trailing slash in proxy_pass
Section titled “The trailing slash in proxy_pass”This one breaks everyone’s brain once, and then it breaks it again a year later.
location /api/ { proxy_pass http://127.0.0.1:8080; # no URI part}# /api/users -> http://127.0.0.1:8080/api/users (path passed through unchanged)
location /api/ { proxy_pass http://127.0.0.1:8080/; # has a URI part ("/")}# /api/users -> http://127.0.0.1:8080/users (location prefix replaced)If proxy_pass has anything after the host:port, even a single /, nginx swaps the matched location prefix for it. If it doesn’t, the original URI goes through untouched. Decide which you want, and keep the slashes on the location and the proxy_pass consistent.
HTTPS with Let’s Encrypt
Section titled “HTTPS with Let’s Encrypt”sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d app.example.comsudo certbot renew --dry-run # check renewal works BEFORE the 90 days are upsystemctl list-timers | grep certbotCertbot edits your server block for you. If you’d rather write it yourself (I would):
server { listen 80; listen [::]:80; server_name app.example.com; return 301 https://$host$request_uri;}
server { listen 443 ssl; listen [::]:443 ssl; http2 on; 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;
add_header Strict-Transport-Security "max-age=31536000" always;
location / { proxy_pass http://127.0.0.1:8080; include snippets/proxy-headers.conf; }}http2 on; is the current syntax. The old listen 443 ssl http2; still works on older versions but logs a deprecation warning on newer ones.
Use fullchain.pem, not cert.pem. With cert.pem, browsers work (they fill in the intermediate themselves), while curl, Java, and every mobile app in the world fail. Then you spend an afternoon convinced it’s a client bug.
WebSockets
Section titled “WebSockets”Websocket apps “connect then immediately drop” behind nginx until you add this:
# in the http {} block (e.g. /etc/nginx/conf.d/websocket-map.conf)map $http_upgrade $connection_upgrade { default upgrade; '' close;}location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 1h; # default 60s kills idle sockets include snippets/proxy-headers.conf;}nginx in Docker
Section titled “nginx in Docker”If nginx runs in a container and proxies to other containers, put them on a shared network and proxy by service name:
location / { proxy_pass http://app:8000;}The catch: nginx resolves that hostname once at startup. If app isn’t up yet, nginx refuses to start (host not found in upstream). If app restarts and gets a new IP, nginx keeps sending traffic to the old one. Force re-resolution with Docker’s embedded DNS and a variable:
resolver 127.0.0.11 valid=10s;
location / { set $upstream http://app:8000; proxy_pass $upstream;}Using a variable in proxy_pass changes the URI handling. The path is passed as-is and the trailing slash trick from above stops working. Everything has a price. See Docker Networks for the network side of this.
Reading 50x errors
Section titled “Reading 50x errors”| Code | Usually means |
|---|---|
| 502 Bad Gateway | Upstream isn’t there: app is down, wrong port, localhost vs 127.0.0.1, container not on the network. |
| 504 Gateway Timeout | Upstream is there but too slow. App’s stuck, or raise proxy_read_timeout if the slowness is legitimate. |
| 413 Request Entity Too Large | client_max_body_size. Default 1m. Someone tried to upload a photo. |
| 499 (in logs only) | The client gave up before you answered. Usually a symptom of slowness, not a cause. |
The error log says which one and why:
sudo tail -f /var/log/nginx/error.logconnect() failed (111: Connection refused) while connecting to upstream means nothing is listening where you pointed it. Go check with ss -tulpn. Then check that you saved the file. Then check that you reloaded. Then check you’re on the right server.