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.
- Hide the version.
server_tokens off;inhttp {}. To also removeServer: nginxentirely, install theheaders_moremodule andmore_clear_headers Server;. - Lock down listeners. Every
listenline should bind to a specific port, and IPv6 (listen [::]:443 ssl http2;) should match IPv4 — do not let v6 silently work without TLS. - 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; }444closes the connection without a response;ssl_reject_handshake on;(Nginx 1.19.4+) prevents TLS-handshake fingerprinting on unmatched names. - 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 offremoves a forward-secrecy weakness. See Mozilla’sssl-config.mozilla.orgfor the current intermediate-profile template. - HSTS (see Nginx security headers) — at least
max-age=31536000; includeSubDomainsonce you are sure every subdomain has TLS. - 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. - 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. - 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; - No directory listing.
autoindex off;is the default; double-check nothing turns it on. - Block access to dot-files and backups:
location ~ /\. { deny all; access_log off; log_not_found off; } location ~ ~$ { deny all; } - HTTP method restriction. If the upstream only handles GET/POST/HEAD, return 405 for the rest:
if ($request_method !~ ^(GET|HEAD|POST)$) { return 405; } - Drop the default site.
sudo rm /etc/nginx/sites-enabled/defaultonce you have a real server block. - Rate limit the abuse-prone endpoints. See limit_req for the patterns.
error_loglevel.warnin production.infoonly for explicit troubleshooting; revert when done.- Process user. Nginx workers should run as a non-root user (
www-datais the default). Verify withps -ef | grep nginx. - File permissions.
/etc/nginx/should be0755, files0644, secrets (e.g. ssl keys)0600owned by root. - 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.
- 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 TLSv1for “compatibility”. Anything that needs TLS 1.0 needs to be replaced, not enabled.add_headerrules dropped by a deeperlocation(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_serveronly protects unmatched Host headers; misspelled names still hit the default. Auditserver_names withnginx -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.