How to Secure Nginx With Security Headers in 2026

How to Secure Nginx With Security Headers: The Core Set

Learning how to secure Nginx with security headers is one of the highest-value hardening steps for any web server. Security headers are HTTP response headers that tell browsers how to handle your site: whether it may be framed, which scripts may run, and whether to force HTTPS. This guide shows how to secure Nginx with security headers in 2026, what each header does, and how to roll them out without breaking your site.

Headers do not fix vulnerable code, but they shrink what an attacker can do with a vulnerability. A missing X-Frame-Options header lets an attacker frame your login page. A missing Content Security Policy lets an injected script run freely. These are cheap protections, and every header in this guide is a few lines of configuration. Start by checking what your site currently sends:

curl -I https://example.com | grep -iE "strict-transport|x-frame|x-content|content-security|referrer|permissions-policy"

If the output is empty, your site sends none of these headers, which is the normal state for a fresh Nginx install. The baseline hardening for WordPress sites is covered in the WordPress security basics for VPS owners, and headers are the server-level complement to those application steps.

Set the Core Security Headers

Add these headers to your server block. Each one is explained below the snippet so you can decide whether to keep it:

add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

X-Frame-Options: SAMEORIGIN stops other sites from framing your pages, which blocks clickjacking. X-Content-Type-Options: nosniff stops browsers from guessing a file’s type, which blocks attacks that upload a disguised script as an image. Referrer-Policy controls how much of the referring URL leaks to other sites. Permissions-Policy disables browser features your site does not use, such as the camera and microphone. The Nginx HSTS header, Strict-Transport-Security, tells browsers to use HTTPS for two years and to include subdomains, which stops SSL-stripping attacks.

Be careful with HSTS. Once a browser sees it, it refuses plain HTTP for your domain until the max-age expires, even if you later remove the header. Only enable HSTS with includeSubDomains when every subdomain serves HTTPS, and only add preload when you intend to submit the domain to the HSTS preload list, which is effectively permanent. Test with a short max-age first, such as 300 seconds, and raise it after a week of confirmed HTTPS stability.

The always parameter matters. Without it, Nginx only sends the header on successful responses, so error pages and redirects go out without protection. With it, every response carries the headers. This is a detail that security scanners flag, and it costs nothing to get right.

Add a Content Security Policy Without Breaking the Site

Content Security Policy is the most powerful header and the easiest to break a site with. A strict policy blocks inline scripts, third-party embeds, and external fonts, which is exactly what many WordPress sites rely on. Roll it out in report-only mode first:

add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' https:; frame-ancestors 'self';" always;

A content security policy Nginx configuration like this one starts permissive on styles and images while keeping scripts locked to your own domain. Watch the browser console for violations for a week. Every blocked resource is something your policy must allow or something you should remove. When the console is quiet, switch the header name from Content-Security-Policy-Report-Only to Content-Security-Policy to enforce it. A Nginx security headers configuration that starts strict and breaks the checkout page is worse than a moderate policy that stays enforced, because a broken policy gets removed entirely.

For WordPress sites, expect to allow the domains your plugins and themes actually use: analytics, fonts, video embeds, and payment scripts each need entries. List them explicitly rather than allowing broad wildcards, because a wildcard in script-src defeats most of the policy’s value. The Cloudflare layer can also set headers, and the guide to Cloudflare security settings and SSL modes explains which protections belong at the edge versus the origin. If both layers set the same header, browsers use the first one they see, so keep one layer in charge to avoid confusion.

Test Headers and Keep Them Maintained

After reloading Nginx, verify the headers are actually sent on real pages, not just the homepage:

sudo nginx -t && sudo systemctl reload nginx
curl -I https://example.com/
curl -I https://example.com/a-real-page/

Test an inner page because a location block can override server-level headers, and a page served from a different location may miss them. Run a public header scanner for a second opinion, and recheck after every Nginx upgrade or server block change, because a rewritten configuration can silently drop the header lines.

Headers are one layer among several. They do not replace brute-force protection, which is covered by the Fail2ban setup guide for SSH and Nginx, or TLS configuration, which is covered in the comparison of TLS 1.2 versus TLS 1.3. The right posture is layers: headers for the browser, TLS for the connection, Fail2ban for the attackers, and updates for everything. Set the headers, verify them, and revisit them when the site gains new third-party scripts, because a policy written for today’s site will be wrong for next year’s.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.