Skip to content

Nginx hardening checklist — the production posture we apply on every host

The Nginx hardening items we go through on every server — version hiding, TLS posture, body limits, timeouts, default-server, and the rest of the items that make a baseline.

This is the Nginx hardening checklist we walk through on every host we onboard. None of these items is novel by itself — security headers, TLS posture, body limits, default_server, version hiding — but together they constitute a posture that a generic install does not have. The list is opinionated, and the values reflect what we have found works in production over many engagements rather than a vendor’s “secure by default” wish.

How to verify

Run a quick audit on the host:

sudo nginx -V 2>&1 | tr ' ' '\n' | grep -E '^--with|module'
sudo nginx -T | grep -E 'server_tokens|client_max_body_size|ssl_protocols|listen|add_header'
curl -sI https://example.com/ | head -20
curl -sI https://example.com/.git/config        # exposure check
sudo grep -E 'server_tokens off|server_name _' /etc/nginx/nginx.conf
sslscan example.com:443 | head -30                # external view

sslscan (or testssl.sh) gives the external TLS view. The exposure-check curl should not return 200.

What’s happening

The default Nginx install is permissive — it tells the world the version, accepts arbitrary Host headers, accepts large request bodies, supports old TLS versions, and ships with a “It Works!” page. Hardening is the set of small adjustments that close off the easy attacks and limit the blast radius of the harder ones. None of these are silver bullets; together they make the host less interesting to opportunistic abuse and more comprehensible to the next operator.

The procedure

Walk these items in order. Each is one line of config or one decision.

  1. Hide the version. server_tokens off; in http {}. To also remove Server: nginx entirely, install the headers_more module and more_clear_headers Server;.
  2. Lock down listeners. Every listen line should bind to a specific port, and IPv6 (listen [::]:443 ssl http2;) should match IPv4 — do not let v6 silently work without TLS.
  3. Set a deliberate default_server. Exactly one server block per listener carries it, and for hardened deployments the default is a no-content closer:
    server {
        listen 80 default_server;
        listen 443 ssl http2 default_server;
        server_name _;
        ssl_reject_handshake on;
        return 444;
    }
    444 closes the connection without a response; ssl_reject_handshake on; (Nginx 1.19.4+) prevents TLS-handshake fingerprinting on unmatched names.
  4. TLS posture. Disable TLS 1.0/1.1, prefer modern ciphers:
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;
    ssl_session_tickets off removes a forward-secrecy weakness. See Mozilla’s ssl-config.mozilla.org for the current intermediate-profile template.
  5. HSTS (see Nginx security headers) — at least max-age=31536000; includeSubDomains once you are sure every subdomain has TLS.
  6. Security headers — X-Content-Type-Options nosniff, Referrer-Policy strict-origin-when-cross-origin, CSP per app, Permissions-Policy. The full set is in the security-headers article.
  7. Body size limit. Default client_max_body_size 1m; is fine for most sites; raise per-location for upload endpoints. A box with no upload endpoint at all should be even tighter.
  8. Timeouts. Stop slow-client attacks (Slowloris) and runaway uploads:
    client_body_timeout    10s;
    client_header_timeout  10s;
    send_timeout           10s;
    keepalive_timeout      30s 30s;
    reset_timedout_connection on;
  9. No directory listing. autoindex off; is the default; double-check nothing turns it on.
  10. Block access to dot-files and backups:
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    location ~ ~$ {
        deny all;
    }
  11. HTTP method restriction. If the upstream only handles GET/POST/HEAD, return 405 for the rest:
    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 405;
    }
  12. Drop the default site. sudo rm /etc/nginx/sites-enabled/default once you have a real server block.
  13. Rate limit the abuse-prone endpoints. See limit_req for the patterns.
  14. error_log level. warn in production. info only for explicit troubleshooting; revert when done.
  15. Process user. Nginx workers should run as a non-root user (www-data is the default). Verify with ps -ef | grep nginx.
  16. File permissions. /etc/nginx/ should be 0755, files 0644, secrets (e.g. ssl keys) 0600 owned by root.
  17. Log shipping. Local-only logs are an audit gap. Ship to a central log store; see Nginx log formats for the JSON format we use.
  18. Patch cadence. Unattended-upgrades for the distro nginx, or your own pipeline for mainline.

After each change: sudo nginx -t && sudo systemctl reload nginx.

Common pitfalls

  • Following an outdated cipher list. The Mozilla profiles update; pin yours to a template, not a hand-typed list.
  • ssl_protocols TLSv1 for “compatibility”. Anything that needs TLS 1.0 needs to be replaced, not enabled.
  • add_header rules dropped by a deeper location (see security headers article) — the hardening is invisible to the request that actually matters.
  • Setting client_max_body_size 100m; globally to “make uploads work”. The whole site now accepts 100 MB POSTs. Scope it per-location.
  • Forgetting that default_server only protects unmatched Host headers; misspelled names still hit the default. Audit server_names with nginx -T | grep server_name.

In the engagements we run, this checklist is encoded in the Ansible role that owns Nginx config — a host that drifts from baseline is detected, not just hoped against. Stack Harbor’s managed operations maintain this posture across every host we run, with the values reviewed quarterly against the current best-practice templates.