Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 5 min read

Nginx Hardening Checklist

Nginx ships with sensible defaults, but a default install is not a hardened one. Version numbers leak in headers, old virtual hosts answer for any hostname, security headers vanish on error pages, and upload limits are w

Nginx ships with sensible defaults, but a default install is not a hardened one. Version numbers leak in headers, old virtual hosts answer for any hostname, security headers vanish on error pages, and upload limits are whatever someone set three years ago. This nginx hardening checklist walks through the checks worth running on any server you are responsible for, with short config snippets and a note on how to verify each one.

Everything here is for servers you own or are authorised to manage. Make changes on staging first, and run nginx -t before every reload.

Before you start: see the config nginx actually runs

Most mistakes come from editing one file while another one wins. Two commands help:

nginx -V   # version, TLS library and compile-time modules
nginx -T   # test the config and print every included file

Save the output of nginx -T before you change anything. It is your rollback reference and makes it easy to diff the result later.

The nginx hardening checklist

1. Keep nginx and OpenSSL patched

Most real nginx risk comes from outdated packages, not exotic settings. Check nginx -V against the current stable and mainline releases on nginx.org and your distribution's security advisories. TLS behaviour depends on the library nginx was built with, so patch OpenSSL too.

2. Stop advertising the version

By default server_tokens is on, which puts the nginx version on error pages and in the Server header (core module docs).

http {
    server_tokens off;
}

This does not fix anything by itself, but it stops you handing out an inventory of versions to anyone who asks.

3. Use modern TLS only

Since nginx 1.23.4 the default ssl_protocols is TLSv1.2 TLSv1.3 (SSL module docs). Older configs often list TLS 1.0 and 1.1 explicitly, which overrides the safer default. Search for it:

nginx -T | grep -n ssl_protocols

If you find old protocols, set it explicitly:

ssl_protocols TLSv1.2 TLSv1.3;

For cipher suites, the Mozilla SSL Configuration Generator gives tested profiles for your nginx and OpenSSL versions. The "intermediate" profile is a reasonable default unless you know every client supports TLS 1.3.

4. Redirect HTTP and send HSTS

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    # ssl_certificate and ssl_certificate_key go here
    add_header Strict-Transport-Security "max-age=31536000" always;
}

Start with a short max-age while you test, and only add includeSubDomains once every subdomain serves HTTPS.

5. Add security headers, and know how they inherit

This is the nginx security best practice that trips up experienced admins. add_header directives are inherited from the previous level "if and only if there are no add_header directives defined on the current level" (headers module docs). One add_header inside a location silently drops every header set at the server level for that location.

Two more details from the same page: without the always parameter, headers are only added to responses such as 200, 204, 301, 302 or 304, so your error pages go out without them. And nginx 1.29.3 added an add_header_inherit directive that can merge headers across levels, if your version has it.

add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "frame-ancestors 'self'" always;

A simple habit: keep headers in one snippet file and include it in every block that sets its own add_header.

6. Add a default server that refuses unknown hosts

If a request arrives with a Host header that no server block matches, nginx uses the default server for that listen socket, often the first block it loaded. That can expose an internal app on a hostname you never meant to publish.

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    server_name _;
    ssl_reject_handshake on;
    return 444;
}

ssl_reject_handshake (nginx 1.19.4 and later) refuses the TLS handshake, so this block does not need a certificate.

7. Keep directory listings off and hide dotfiles

autoindex is off by default; make sure nobody turned it on for a convenience download folder. Then block hidden files such as .git or .env that might end up in a web root, while still allowing .well-known for certificate renewal:

location ~ /\.(?!well-known) {
    deny all;
}

8. Set request size and rate limits

client_max_body_size defaults to 1m. Set it per location to what the app needs, not a large global value. For login and API endpoints, a rate limit takes the edge off brute-force attempts and scraping:

http {
    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
}

location /login {
    limit_req zone=login burst=5 nodelay;
}

These numbers are examples. Tune them with your own traffic.

9. Restrict admin and status endpoints

Pages like stub_status, admin panels or metrics should not be public.

location /nginx_status {
    stub_status;
    allow 10.0.0.0/8;
    deny all;
}

10. Run workers unprivileged and lock down files

The master process starts as root to bind ports; workers should run as an unprivileged user (user www-data; or user nginx; depending on your distribution). Config files and private keys should be readable by root only, and the web root should not be writable by the worker user unless the app truly needs it.

11. Watch proxy and alias settings

If nginx sits in front of an app, forward only the headers the app needs, and make sure the app trusts X-Forwarded-For only from your proxy. When you use alias, keep trailing slashes consistent between the location and the path, as in the official example location /i/ { alias /data/w3/images/; }. The follow-up article on common nginx misconfigurations covers these in more depth.

12. Log enough to investigate

Keep access and error logs on, rotated and shipped somewhere you can search. A hardening change you cannot observe is hard to trust.

Verify from the outside

A checklist is only half the job. After each change, test from a machine outside your network:

  • curl -sI https://example.com/ shows your headers; repeat it on a URL that returns 404.
  • curl -skI -H "Host: unknown.example" https://your-server-ip/ should fail or close without serving a site.
  • A TLS checker such as SSL Labs confirms protocols and certificate chains.
  • The CIS NGINX Benchmark is a longer reference if you need a formal baseline.

How to secure nginx over time

Hardening drifts. New virtual hosts get added, certificates expire, someone copies an old config block from a wiki. Put the config in version control, review diffs, and re-run the outside checks on a schedule rather than once.

That is where continuous monitoring helps. CerberusGuard discovers hosts and certificates across your domains, alerts on new hosts and on expiring or untrusted TLS certificates, and runs authorised web scans with Nuclei and OWASP ZAP. Every scan checks a signed authorisation for your scope first. If you want to keep an eye on your exposure between manual reviews, take a look at CerberusGuard.

Originally published at CerberusGuard blog.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.