Server Administration • Quick Reference

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.

⏱️ 8 min reference🟢 Beginner → SysAdmin📅 Updated: 2026

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.

1. Front Door HTTPS :443 Accepts public traffic & terminates SSL/TLS
2. Header Injection X-Forwarded-* Tells the backend the real client IP & protocol
3. Upstream Handoff 127.0.0.1:8080 Forwards traffic to your local app over HTTP
Memory Trick: A Forward Proxy hides the client from the internet (e.g., corporate VPN/office proxy). A Reverse Proxy hides the server from the internet (e.g., Nginx/Apache protecting your backend apps).
PUBLIC INTERNET Browser / Client https://app.site.com Encrypted TLS 1.3 :443 TLS TERMINATION Nginx / Apache Proxy 1. Decrypts SSL (Certbot) 2. Sets X-Forwarded-For 3. Adds Security Headers proxy_pass → :8080 HTTP PRIVATE LOOPBACK Backend App 127.0.0.1:8080 Node / Python / Go Firewall blocks port 8080 externally — only Nginx/Apache can reach the app locally

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!

/etc/nginx/sites-available/myapp.conf
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 -t
Test Nginx configuration syntax Always run this before reloading. Catches missing semicolons or bad paths without touching live traffic.
sudo systemctl reload nginx
Gracefully reload Nginx (zero downtime) Applies new config to fresh worker processes while letting active downloads/requests finish cleanly.
sudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/
Enable a site on Debian/Ubuntu Creates a symbolic link from sites-available into sites-enabled.
sudo tail -f /var/log/nginx/error.log
Watch live Nginx errors First place to look when seeing 502 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.

/etc/apache2/sites-available/myapp.conf
<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 rewrite
Enable required Apache2 reverse proxy modules Turns on HTTP proxying, SSL/TLS, custom header injection, and URL rewriting in one command.
sudo a2ensite myapp.conf
Enable an Apache2 VirtualHost site Use sudo a2dissite 000-default.conf to disable the default welcome page if needed.
sudo apache2ctl -S
Inspect active VirtualHosts & syntax Lists every parsed vhost, port, and config file—great for debugging why the wrong site is loading.
sudo systemctl reload apache2
Reload Apache2 gracefully Run sudo 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.

Prerequisite Check: Before running Certbot, make sure your domain's DNS 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.com
Issue & auto-configure SSL for Nginx Obtains the cert, configures port 443, and sets up HTTP→HTTPS redirect automatically.
sudo certbot --apache -d example.com -d www.example.com
Issue & auto-configure SSL for Apache2 Creates the *-le-ssl.conf VirtualHost and enables HTTPS redirect.
sudo certbot renew --dry-run
Simulate automatic certificate renewal Tests renewal without hitting Let's Encrypt rate limits. Always run this after initial setup!
sudo certbot certificates
List installed certs & expiry dates Shows certificate paths (/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

⚠️ Common Mistakes That Take Down Live Sites
1. Reloading Nginx or Apache without testing syntax first

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.

2. Forgetting 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.

3. Letting SSL certificates expire silently

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.