nginx.ccWeb server troubleshooting desk

nginx config generator: reverse proxy, PHP, WordPress, SPA, load balancing, WebSocket

Generate ready-to-use nginx configs by scenario, with HTTPS, HTTP/2 and security headers, plus Caddy equivalents for the main scenarios.

Nginx · Reverse proxy

Save as /etc/nginx/conf.d/example.com.conf · Apply: nginx -t && systemctl reload nginx
# example.com — generated by nginx.cc · Reverse proxy
# Save as /etc/nginx/conf.d/example.com.conf, then nginx -t && systemctl reload nginx

# These maps are http-level; identical maps repeated by several sites do not conflict
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# upstream keepalive needs an empty Connection header
map $http_upgrade $connection_upgrade_keepalive {
    default upgrade;
    ''      '';
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    # Let's Encrypt validation (renewals use it too)
    location ^~ /.well-known/acme-challenge/ {
        root /var/www/_letsencrypt;
    }

    location / {
        return 301 https://example.com$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Mozilla Intermediate (Mozilla Server Side TLS 6.0)
    ssl_protocols TLSv1.2 TLSv1.3;
    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_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    # No OCSP stapling: Let's Encrypt shut down its OCSP service in 2025.

    server_tokens off;
    client_max_body_size 64m;

    # Security headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    # CSP must be tailored per site; enable it once you are sure it does not block your own scripts:
    # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
    add_header Strict-Transport-Security "max-age=63072000" always;

    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log warn;

    # Hidden files (.git, .env, …)
    location ~ /\.(?!well-known) {
        deny all;
    }

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;

    location / {
        proxy_pass http://127.0.0.1:8384;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        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;
        proxy_set_header X-Forwarded-Port $server_port;
    }
}
Stage-1 config (first deploy, before the certificate exists; HTTP only)
# example.com — generated by nginx.cc · Reverse proxy
# Save as /etc/nginx/conf.d/example.com.conf, then nginx -t && systemctl reload nginx

# These maps are http-level; identical maps repeated by several sites do not conflict
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# upstream keepalive needs an empty Connection header
map $http_upgrade $connection_upgrade_keepalive {
    default upgrade;
    ''      '';
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    server_tokens off;
    client_max_body_size 64m;

    # Security headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    # CSP must be tailored per site; enable it once you are sure it does not block your own scripts:
    # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;

    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log warn;

    # Let's Encrypt validation
    location ^~ /.well-known/acme-challenge/ {
        root /var/www/_letsencrypt;
    }

    # Hidden files (.git, .env, …)
    location ~ /\.(?!well-known) {
        deny all;
    }

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;

    location / {
        proxy_pass http://127.0.0.1:8384;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        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;
        proxy_set_header X-Forwarded-Port $server_port;
    }
}

First deploy

  1. Install or update nginx (nginx.org repo)
    curl -fsSL nginx.cc/install | sudo bash
  2. Stage 1: deploy the HTTP-only config (no certificate yet, the HTTPS one would fail nginx -t)
    sudo mkdir -p /var/www/_letsencrypt
    # Save the stage-1 config below as /etc/nginx/conf.d/example.com.conf
    sudo nginx -t && sudo systemctl reload nginx
  3. Issue a Let's Encrypt certificate (webroot, nginx config untouched)
    sudo certbot certonly --webroot -w /var/www/_letsencrypt -d example.com --cert-name example.com \
      --email you@example.com --agree-tos --no-eff-email \
      --deploy-hook "systemctl reload nginx"
    Alternatively certbot certonly --nginx -d example.com --cert-name example.com (also leaves the config alone); avoid letting certbot --nginx rewrite this file. DNS must point here and port 80 must be open.
  4. Stage 2: switch to the full HTTPS config
    # Overwrite /etc/nginx/conf.d/example.com.conf with the full config
    sudo nginx -t && sudo systemctl reload nginx
  5. Check automatic renewal
    sudo certbot renew --dry-run
  6. Verify after going live
    curl -sI https://example.com | head -20
    curl -fsSL nginx.cc/fix | sudo bash   # read-only health check
    Or run the nginx.cc online diagnostics for headers, redirects and compression.

Caddy equivalent

/etc/caddy/Caddyfile · caddy validate && systemctl reload caddy
example.com {
    encode zstd gzip
    header {
        X-Content-Type-Options nosniff
        X-Frame-Options SAMEORIGIN
        Referrer-Policy strict-origin-when-cross-origin
        Permissions-Policy "camera=(), microphone=(), geolocation=()"
        Strict-Transport-Security "max-age=63072000"
        -Server
    }
    # Caddy handles WebSocket upgrades and X-Forwarded-* headers automatically
    reverse_proxy 127.0.0.1:8384
}

Notes

  • Certificate paths follow certbot defaults (/etc/letsencrypt/live/example.com/). On first deploy follow the steps: HTTP-only config first, then issue the certificate.
  • Bind the backend to 127.0.0.1. Express needs app.set('trust proxy', 1) to see the client IP and https.

Paste the result into the Linter to double-check; for certificates see ssl.sh.

FAQ

Can I use the generated config as-is?

Yes. Option combinations for every scenario are validated with nginx -t in Docker, and the main scenarios get functional tests with real requests; run nginx -t, then systemctl reload nginx.

Does it work with older nginx (Debian 12 / Ubuntu 24.04 packages)?

Yes. Pick “< 1.25.1” as the nginx version to get the old listen 443 ssl http2 syntax; otherwise http2 on fails as an unknown directive.

How does it relate to nginxconfig.io?

The shared-snippet layout is adapted from nginxconfig.io (MIT, DigitalOcean) and the code is rewritten; the full mode covers all of its options, imports its share links and replaces outdated output with modern equivalents.