HTTP Security Headers: What to Set, How to Configure Them and How to Test

HTTP security headers checklist graphic: shield with HSTS, CSP and nosniff labels
HTTP security headers checklist graphic: shield with HSTS, CSP and nosniff labels

Introduction

HTTP security headers are response headers that tell the browser how to treat your pages. They are cheap to add and they limit the damage of bugs such as cross-site scripting (XSS), clickjacking and downgrade attacks. This guide lists the HTTP security headers worth setting as of November 23, 2026, gives copy-ready nginx and Apache examples, and shows how to roll out the risky ones (HSTS and CSP) safely. It follows the OWASP HTTP Headers Cheat Sheet and MDN documentation. No header replaces patching, so keep your software updated too.

Key Facts: HTTP Security Headers at a Glance

  • Set on every HTML response: Strict-Transport-Security, Content-Security-Policy, X-Content-Type-Options, Referrer-Policy, Permissions-Policy.
  • Framing: use CSP frame-ancestors. OWASP says it obsoletes X-Frame-Options; X-Frame-Options: DENY is the fallback.
  • Do not rely on: X-XSS-Protection (OWASP: set 0 or omit), Expect-CT and Public-Key-Pins (HPKP; OWASP: do not use).
  • Riskiest to deploy: HSTS (mistakes lock users out) and CSP (can break scripts). Roll both out in stages.
  • Nginx gotcha: add_header is inherited only if the current level defines no add_header of its own.
  • Test with: curl -I, browser DevTools and the Mozilla Observatory.

Which HTTP Security Headers Should You Set?

Set the headers below on pages that browsers render. The values come from the OWASP HTTP Headers Cheat Sheet.

HeaderRecommended value (OWASP)What it does
Strict-Transport-Securitymax-age=63072000; includeSubDomains; preloadMakes browsers use HTTPS only
Content-Security-PolicySee the CSP section belowLimits where scripts and other content load from
X-Content-Type-OptionsnosniffStops MIME type sniffing
Referrer-Policystrict-origin-when-cross-originLimits referrer data sent to other sites
Permissions-Policygeolocation=(), camera=(), microphone=()Disables browser features you do not use
Cross-Origin-Opener-Policysame-originIsolates your page from cross-origin windows
Cross-Origin-Resource-Policysame-siteLimits which origins can load your resources
X-Frame-OptionsDENYBlocks framing (legacy; prefer CSP frame-ancestors)

OWASP notes that CSP, COOP and similar headers matter for pages browsers render, and may be meaningless on a JSON API. Adjust the Permissions-Policy list to the features your site really needs.

What Is HSTS and How Do You Roll It Out Safely?

HSTS (HTTP Strict Transport Security) tells a browser to reach your site only over HTTPS. Browsers honor it only when it arrives over HTTPS (RFC 6797 forbids sending it over plain HTTP).

OWASP warns that a misconfigured header or an expired certificate can lock legitimate users out for the whole max-age. MDN therefore suggests a staged rollout with includeSubDomains:

  1. max-age=300; includeSubDomains (5 minutes).
  2. max-age=604800; includeSubDomains (1 week).
  3. max-age=2592000; includeSubDomains (1 month).
  4. Only then raise max-age. For the preload list, max-age must be at least 31536000 (1 year) and both includeSubDomains and preload must be present.

Wait the full max-age of each stage before the next one.

HTTP security headers HSTS staged rollout: 5 minutes, 1 week, 1 month, then at least 1 year for preload
Staged HSTS rollout recommended by MDN, with the 1-year minimum needed for preload.

Preloading is hard to undo, and it covers all subdomains, so submit to hstspreload.org only when every subdomain serves HTTPS.

How Do You Build a Content Security Policy?

A Content Security Policy (CSP) lists which sources may load scripts and other content, so injected scripts are blocked. OWASP’s nonce-based strict policy is a good target:

Content-Security-Policy: script-src 'nonce-{RANDOM}' 'strict-dynamic'; object-src 'none'; base-uri 'none';

The nonce must be a fresh random value for every response and must be added to your own <script> tags by a real templating engine. OWASP warns against a middleware that adds the nonce to all script tags, because injected scripts would get it too. Static HTML cannot use nonces; OWASP offers a hash-based variant for that case.

Start with Content-Security-Policy-Report-Only. MDN explains that this mode does not enforce the policy but reports violations, so you can fix inline code before you enforce. For framing, add frame-ancestors 'none' (or 'self') in the enforced CSP.

How Do You Add HTTP Security Headers in Nginx and Apache?

Use the web server’s header module and the always option so error pages get the headers too. Both examples use values from OWASP; replace the CSP with your tested policy.

Nginx

server {
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "geolocation=(), camera=(), microphone=()" always;
    add_header Cross-Origin-Opener-Policy "same-origin" always;
}

The nginx documentation says add_header directives are inherited from the previous level if and only if the current level defines none. A single add_header inside a location block therefore drops all server-level headers for that location. Since nginx 1.29.3 the add_header_inherit directive (on, off or merge) can change this. Use merge only after checking your nginx version and testing.

Apache HTTP Server

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), camera=(), microphone=()"
</IfModule>

The Apache documentation says the always table adds headers even on errors and keeps them across internal redirects. If your application framework already sets some of these headers, avoid duplicates and set each header in one place.

How Can You Check Which HTTP Security Headers Your Site Sends?

Request only the headers with curl and read the response:

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

Replace example.com with your own domain. Check the home page, a login page and an error page, because a location block or an application can change the headers. The Mozilla Observatory, which OWASP lists for this purpose, scans a site and reports missing headers. Online tools usually test only the home page.

What Else Should You Harden?

  • Remove version leaks. OWASP advises removing X-Powered-By and hiding or genericizing Server, though attackers have other ways to fingerprint a stack.
  • Set Cache-Control for sensitive pages. OWASP recommends no-store for sensitive responses. no-cache does not stop caching; it only requires revalidation. Proxies and filters can change these headers, as the ExpiresFilter bug in our Apache Tomcat security update article shows.
  • Remove obsolete headers. Drop Public-Key-Pins. OWASP says it was removed from Chromium in 2018 and recommends relying on Certificate Transparency and CAA DNS records.
  • Patch the server. Headers reduce impact but do not fix bugs. See our nginx and Apache HTTP Server update guides.
  • Add a WAF in front. It is a second layer, not a substitute.

FAQ

Do HTTP security headers stop XSS completely?

No. A strict CSP is the strongest header-based defense against XSS, but you still need output encoding and input validation. Treat headers as defense in depth.

Is X-XSS-Protection still needed?

No. OWASP recommends a CSP that disables inline JavaScript and says not to set X-XSS-Protection or to set it explicitly to 0. MDN warns it can create XSS holes in otherwise safe sites.

Should I still use X-Frame-Options?

Prefer CSP frame-ancestors, which OWASP says obsoletes it in supporting browsers. Keeping X-Frame-Options: DENY as well is harmless for pages that must never be framed.

Can HSTS break my site?

Yes, if HTTPS breaks or a subdomain lacks HTTPS while includeSubDomains is set. Start with a short max-age and increase it in stages, as described above.

Do I need these headers on API responses?

Mostly not all of them. OWASP notes CSP, COOP and related headers may be meaningless on JSON responses that browsers do not render. X-Content-Type-Options: nosniff, correct Content-Type and HSTS still make sense.

How often should I review my security headers?

Review them after every server, proxy or framework change. Browser support and recommendations change, so recheck the OWASP cheat sheet. Values here were verified on November 23, 2026.

Conclusion

HTTP security headers take an afternoon to add and cut the impact of common web attacks. Start with HSTS (staged), nosniff, a Referrer-Policy and Permissions-Policy, then move CSP from report-only to enforced. Test every change with curl and the Mozilla Observatory.

References

All links below were checked on November 23, 2026.

Related Articles

Last Verified: November 23, 2026. Prepared by SafeWebAcademy from OWASP, MDN, IETF, nginx and Apache documentation linked above. Header recommendations can change; recheck the sources before deploying.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *