Nginx, Apache & SSL/TLS Reverse Proxy Cheat Sheet
Your production-ready reference for putting Nginx or Apache in front of Node.js, Python, Go, or Java apps—handling HTTPS termination, forwarding client headers, and keeping certificates auto-renewed.
What Does a Reverse Proxy Actually Do?
Instead of exposing your app directly on port 80 or 443, a reverse proxy sits at the front door, handles encryption, and forwards clean requests to your backend app.
Figure 1: TLS Termination at the Reverse Proxy — public traffic hits port 443, then travels unencrypted over localhost to port 8080.
1 Nginx Server Block & proxy_pass
In Debian/Ubuntu, configs live in /etc/nginx/sites-available/ and are activated by symlinking into sites-enabled/. Always test syntax before reloading!
server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; # Essential forwarding headers 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; # WebSocket support (optional but safe) proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
sudo nginx -tsudo systemctl reload nginxsudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/sites-available into sites-enabled.
sudo tail -f /var/log/nginx/error.log502 Bad Gateway or permission denied errors.
2 Apache2 VirtualHost & Modules
Apache2 requires enabling proxy modules before ProxyPass works. On Debian/Ubuntu, use a2enmod and a2ensite instead of manually symlinking files.
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ProxyPreserveHost On ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ RequestHeader set X-Forwarded-Proto "http" ErrorLog ${APACHE_LOG_DIR}/myapp-error.log CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined </VirtualHost>
sudo a2enmod proxy proxy_http ssl headers rewritesudo a2ensite myapp.confsudo a2dissite 000-default.conf to disable the default welcome page if needed.
sudo apache2ctl -Ssudo systemctl reload apache2sudo apache2ctl configtest first to confirm Syntax OK before reloading.
3 Let's Encrypt certbot SSL/TLS Commands
Certbot automates free 90-day TLS certificates from Let's Encrypt, edits your Nginx/Apache config to add HTTPS on port 443, and sets up auto-renewal.
A record points to your server's public IP and firewall ports 80 & 443 are open (sudo ufw allow 'Nginx Full').
sudo certbot --nginx -d example.com -d www.example.comsudo certbot --apache -d example.com -d www.example.com*-le-ssl.conf VirtualHost and enables HTTPS redirect.
sudo certbot renew --dry-runsudo certbot certificates/etc/letsencrypt/live/...), domains, and days remaining.
4 Essential Security Headers & HTTP Status Codes
Add these hardening headers inside your Nginx server {} block (or Apache VirtualHost with Header always set) to protect users from clickjacking, MIME sniffing, and downgrade attacks.
| Security Header | Recommended Value | What It Protects Against |
|---|---|---|
Strict-Transport-Security |
max-age=31536000; includeSubDomains |
Forces browsers to use HTTPS only (prevents SSL stripping). |
X-Frame-Options |
SAMEORIGIN |
Blocks external sites from embedding your app in an iframe (clickjacking). |
X-Content-Type-Options |
nosniff |
Stops browsers from guessing file types (prevents MIME-confusion XSS). |
Referrer-Policy |
strict-origin-when-cross-origin |
Prevents leaking full URLs/query tokens to third-party sites. |
HTTP Status Codes (Reverse Proxy Troubleshooting)
| Code | Meaning | Reverse Proxy Cause & Quick Fix |
|---|---|---|
| 200 | OK | Proxy and backend app both succeeded normally. |
| 301 | Moved Permanently | Expected for HTTP → HTTPS redirects. Watch out for redirect loops if X-Forwarded-Proto is missing. |
| 403 | Forbidden | Check file permissions, IP allow/deny rules, or SELinux blocking proxy connections. |
| 404 | Not Found | Check trailing slashes in proxy_pass or try_files in Nginx. |
| 413 | Payload Too Large | File upload exceeds Nginx default 1m limit. Raise client_max_body_size 50M;. |
| 502 | Bad Gateway | Backend app is down or listening on the wrong port. Check systemctl status your-app and ss -tulpn. |
| 504 | Gateway Timeout | Backend app took too long to respond. Increase proxy_read_timeout 120s; or fix slow DB queries. |
5 Danger Zone: Reverse Proxy Mistakes to Avoid
A single typo or missing semicolon in /etc/nginx/ or /etc/apache2/ can fail on restart and drop every hosted site. Always run sudo nginx -t or sudo apache2ctl configtest before reloading.
X-Forwarded-For, Host, and X-Forwarded-Proto headers
Without these headers, your backend app thinks every visitor comes from 127.0.0.1, breaks OAuth/CSRF checks, and may trigger infinite HTTP→HTTPS redirect loops.
Blocking port 80 in your firewall breaks Let's Encrypt HTTP-01 auto-renewal. Verify sudo certbot renew --dry-run passes and confirm systemctl status certbot.timer is active.