Nginx Reverse Proxy, Automated SSL Certificates, and Security Hardening Guide

Nginx Reverse Proxy, Automated SSL Certificates, and Security Hardening Guide
nginx
security
ssl
https
devops
linux
Attain an SSL Labs A+ rating with TLS 1.3, OCSP Stapling, token bucket rate limiting, and defensive production HTTP security headers.

In production web architectures, Nginx operates as the critical reverse proxy gateway handling TLS termination, connection throttling, static asset distribution, and defensive perimeter shielding.

Default web server configurations routinely expose legacy cryptographic ciphers, lack HTTP security headers, and leave unthrottled endpoints vulnerable to layer-7 denial-of-service floods. This guide establishes a battle-tested hardening blueprint.


1. SSL Labs A+ Hardening Configuration

Create /etc/nginx/conf.d/security.conf:

# Enforce modern, secure TLS specifications
ssl_protocols TLSv1.2 TLSv1.3;

# Prioritize forward-secrecy AEAD ciphers
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;

# SSL Session optimizations
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;

# OCSP Stapling: Accelerates TLS handshakes by up to 30%
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 8.8.8.8 valid=300s;
resolver_timeout 5s;

# Mandatory HTTP Security Headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

2. Leaky-Bucket Rate Limiting

Protect authentication and compute-intensive endpoints against abusive automation:

In /etc/nginx/nginx.conf:

# General API limiter: 10 requests per second per IP
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;

# Sensitive auth limiter: 1 request per second
limit_req_zone $binary_remote_addr zone=strict_limit:10m rate=1r/s;

In your server block:

server {
    listen 443 ssl http2;
    server_name xptdev.com;

    ssl_certificate /etc/letsencrypt/live/xptdev.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xptdev.com/privkey.pem;

    include /etc/nginx/conf.d/security.conf;

    location /api/auth/ {
        limit_req zone=strict_limit burst=5 nodelay;
        proxy_pass http://127.0.0.1:3000;
        include proxy_params;
    }

    location / {
        limit_req zone=api_limit burst=20 nodelay;
        proxy_pass http://127.0.0.1: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;
    }
}

3. Automated TLS Lifecycle via Certbot

Deploy automated Let's Encrypt certificate renewal:

sudo apt update && sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d xptdev.com -d www.xptdev.com
sudo certbot renew --dry-run

Certbot registers automated systemd timers to seamlessly renew certificates within 30 days of expiration without service disruption.