<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: CerberusGuard</title>
    <description>The latest articles on DEV Community by CerberusGuard (@cerberusguard).</description>
    <link>https://dev.to/cerberusguard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4259083%2F3eb31e15-c1cd-4640-86f9-ccb077035f0e.png</url>
      <title>DEV Community: CerberusGuard</title>
      <link>https://dev.to/cerberusguard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cerberusguard"/>
    <language>en</language>
    <item>
      <title>Nginx Hardening Checklist</title>
      <dc:creator>CerberusGuard</dc:creator>
      <pubDate>Thu, 26 Nov 2026 19:18:24 +0000</pubDate>
      <link>https://dev.to/cerberusguard/nginx-hardening-checklist-56fa</link>
      <guid>https://dev.to/cerberusguard/nginx-hardening-checklist-56fa</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Everything here is for servers you own or are authorised to manage. Make changes on staging first, and run &lt;code&gt;nginx -t&lt;/code&gt; before every reload.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before you start: see the config nginx actually runs
&lt;/h2&gt;

&lt;p&gt;Most mistakes come from editing one file while another one wins. Two commands help:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nginx -V   &lt;span class="c"&gt;# version, TLS library and compile-time modules&lt;/span&gt;
nginx -T   &lt;span class="c"&gt;# test the config and print every included file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the output of &lt;code&gt;nginx -T&lt;/code&gt; before you change anything. It is your rollback reference and makes it easy to diff the result later.&lt;/p&gt;

&lt;h2&gt;
  
  
  The nginx hardening checklist
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Keep nginx and OpenSSL patched
&lt;/h3&gt;

&lt;p&gt;Most real nginx risk comes from outdated packages, not exotic settings. Check &lt;code&gt;nginx -V&lt;/code&gt; against the current stable and mainline releases on &lt;a href="https://nginx.org/en/download.html" rel="noopener noreferrer"&gt;nginx.org&lt;/a&gt; and your distribution's security advisories. TLS behaviour depends on the library nginx was built with, so patch OpenSSL too.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Stop advertising the version
&lt;/h3&gt;

&lt;p&gt;By default &lt;code&gt;server_tokens&lt;/code&gt; is &lt;code&gt;on&lt;/code&gt;, which puts the nginx version on error pages and in the &lt;code&gt;Server&lt;/code&gt; header (&lt;a href="https://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens" rel="noopener noreferrer"&gt;core module docs&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;http&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;server_tokens&lt;/span&gt; &lt;span class="n"&gt;off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does not fix anything by itself, but it stops you handing out an inventory of versions to anyone who asks.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Use modern TLS only
&lt;/h3&gt;

&lt;p&gt;Since nginx 1.23.4 the default &lt;code&gt;ssl_protocols&lt;/code&gt; is &lt;code&gt;TLSv1.2 TLSv1.3&lt;/code&gt; (&lt;a href="https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_protocols" rel="noopener noreferrer"&gt;SSL module docs&lt;/a&gt;). Older configs often list TLS 1.0 and 1.1 explicitly, which overrides the safer default. Search for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nginx -T &lt;span class="o"&gt;|&lt;/span&gt; grep -n ssl_protocols
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you find old protocols, set it explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;ssl_protocols&lt;/span&gt; &lt;span class="nc"&gt;TLSv1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nc"&gt;TLSv1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For cipher suites, the &lt;a href="https://ssl-config.mozilla.org/" rel="noopener noreferrer"&gt;Mozilla SSL Configuration Generator&lt;/a&gt; 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Redirect HTTP and send HSTS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;301&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//$host$request_uri;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="n"&gt;ssl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;# ssl_certificate and ssl_certificate_key go here
&lt;/span&gt;    &lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Strict&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Transport&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Security&lt;/span&gt; &lt;span class="s2"&gt;"max-age=31536000"&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with a short &lt;code&gt;max-age&lt;/code&gt; while you test, and only add &lt;code&gt;includeSubDomains&lt;/code&gt; once every subdomain serves HTTPS.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Add security headers, and know how they inherit
&lt;/h3&gt;

&lt;p&gt;This is the nginx security best practice that trips up experienced admins. &lt;code&gt;add_header&lt;/code&gt; directives are inherited from the previous level "if and only if there are no &lt;code&gt;add_header&lt;/code&gt; directives defined on the current level" (&lt;a href="https://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header" rel="noopener noreferrer"&gt;headers module docs&lt;/a&gt;). One &lt;code&gt;add_header&lt;/code&gt; inside a &lt;code&gt;location&lt;/code&gt; silently drops every header set at the &lt;code&gt;server&lt;/code&gt; level for that location.&lt;/p&gt;

&lt;p&gt;Two more details from the same page: without the &lt;code&gt;always&lt;/code&gt; 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 &lt;code&gt;add_header_inherit&lt;/code&gt; directive that can merge headers across levels, if your version has it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Options&lt;/span&gt; &lt;span class="s2"&gt;"nosniff"&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Referrer&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt; &lt;span class="s2"&gt;"strict-origin-when-cross-origin"&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;add_header&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Security&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt; &lt;span class="s2"&gt;"frame-ancestors 'self'"&lt;/span&gt; &lt;span class="n"&gt;always&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simple habit: keep headers in one snippet file and &lt;code&gt;include&lt;/code&gt; it in every block that sets its own &lt;code&gt;add_header&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Add a default server that refuses unknown hosts
&lt;/h3&gt;

&lt;p&gt;If a request arrives with a &lt;code&gt;Host&lt;/code&gt; 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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="n"&gt;default_server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="n"&gt;ssl&lt;/span&gt; &lt;span class="n"&gt;default_server&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ssl_reject_handshake&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;444&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ssl_reject_handshake&lt;/code&gt; (nginx 1.19.4 and later) refuses the TLS handshake, so this block does not need a certificate.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Keep directory listings off and hide dotfiles
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?!&lt;/span&gt;&lt;span class="n"&gt;well&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;known&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;deny&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Set request size and rate limits
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;client_max_body_size&lt;/code&gt; defaults to &lt;code&gt;1m&lt;/code&gt;. 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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;http&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;limit_req_zone $binary_remote_addr zone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10m&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5r&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;limit_req zone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt; &lt;span class="n"&gt;burst&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;nodelay&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These numbers are examples. Tune them with your own traffic.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Restrict admin and status endpoints
&lt;/h3&gt;

&lt;p&gt;Pages like &lt;code&gt;stub_status&lt;/code&gt;, admin panels or metrics should not be public.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;nginx_status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;stub_status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;allow&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;deny&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. Run workers unprivileged and lock down files
&lt;/h3&gt;

&lt;p&gt;The master process starts as root to bind ports; workers should run as an unprivileged user (&lt;code&gt;user www-data;&lt;/code&gt; or &lt;code&gt;user nginx;&lt;/code&gt; 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  11. Watch proxy and alias settings
&lt;/h3&gt;

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

&lt;h3&gt;
  
  
  12. Log enough to investigate
&lt;/h3&gt;

&lt;p&gt;Keep access and error logs on, rotated and shipped somewhere you can search. A hardening change you cannot observe is hard to trust.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify from the outside
&lt;/h2&gt;

&lt;p&gt;A checklist is only half the job. After each change, test from a machine outside your network:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;curl -sI https://example.com/&lt;/code&gt; shows your headers; repeat it on a URL that returns 404.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;curl -skI -H "Host: unknown.example" https://your-server-ip/&lt;/code&gt; should fail or close without serving a site.&lt;/li&gt;
&lt;li&gt;A TLS checker such as &lt;a href="https://www.ssllabs.com/ssltest/" rel="noopener noreferrer"&gt;SSL Labs&lt;/a&gt; confirms protocols and certificate chains.&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://www.cisecurity.org/benchmark/nginx" rel="noopener noreferrer"&gt;CIS NGINX Benchmark&lt;/a&gt; is a longer reference if you need a formal baseline.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to secure nginx over time
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://cerberusguard.example/?utm_source=devto&amp;amp;utm_medium=blog&amp;amp;utm_campaign=launch_m1&amp;amp;utm_content=d001-article" rel="noopener noreferrer"&gt;CerberusGuard&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://cerberusguard.example/blog/nginx-hardening-checklist" rel="noopener noreferrer"&gt;CerberusGuard blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>devops</category>
      <category>sysadmin</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
