X
X
X
X

HTTP Security Headers Explained: nosniff, X-Frame-Options and CSP

HomepageArticlesWeb Security & SSLHTTP Security Headers Explained: no...

In short: HTTP security headers are small instructions your server attaches to every response, telling the browser to "run this page under these rules". A few lines of configuration can reduce risks such as clickjacking, MIME-type confusion and data leakage, but a badly set header can break your site, so roll them out step by step.

When people think about securing a website, they usually think of the SSL certificate and strong passwords. Yet there is another cheap and effective layer on the browser side: response headers. When the server sends a page, it also sends rules such as "this page must not be shown inside a frame on another site" or "do not guess the content type", and the browser obeys them. This article covers the most common headers, what they do and how to add them on your hosting account.

What is a security header and how does it work?

When a browser requests a page, the server first sends the response headers and then the HTML. Security headers live in that first part and restrict what the browser will do. They do not change your site's code; they only tell the browser which actions are allowed. That is why they can be applied at web server level whether you run WordPress, custom software or a static site.

To see where you stand, one command in a terminal is enough:

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

If nothing is printed, none of these headers is set on your site.

Diagram showing security headers added by the server to a response and enforced as rules in the browser

The most important headers

1. Strict-Transport-Security (HSTS)

Tells the browser to connect to your site over HTTPS only. See What is HSTS? for details. If you have not finished moving to HTTPS, follow the HTTP to HTTPS migration guide first, because enabling HSTS during a half-finished migration can lock visitors out.

2. X-Content-Type-Options

Its only valid value is nosniff. The browser trusts the declared Content-Type instead of inspecting the file and guessing, which prevents a file that looks like an image from being executed as a script. It is one of the safest headers to add right away.

3. X-Frame-Options and frame-ancestors

Controls whether your page may be displayed inside an iframe on another site. An attacker can place an invisible frame under a fake button and trick visitors into clicking it; this is called clickjacking. Values:

  • DENY: the page can never be framed.
  • SAMEORIGIN: only pages on your own domain can frame it.

In modern browsers the CSP frame-ancestors directive does the same job, and using both is common. See the MDN documentation for details.

4. Referrer-Policy

Decides which source address (Referer) is sent to the destination when a visitor clicks a link. URLs can contain search terms, user IDs or private paths, so you may not want the full address to leak. A balanced and common value is strict-origin-when-cross-origin: full URL within your own site, only the origin to other sites, and nothing when downgrading from HTTPS to HTTP.

5. Permissions-Policy

Controls whether browser features such as camera, microphone and geolocation can be used by the page or by embedded content. If your site does not need them, turning them off reduces what a compromised third-party script could abuse. Example: camera=(), microphone=(), geolocation=().

6. Content-Security-Policy (CSP)

The most powerful and the most demanding header. It lists the sources from which the page may load scripts, styles, images and fonts. Set up correctly, it can stop an injected malicious script from running (reducing XSS risk). But if you use external services such as ads, analytics, live chat or web fonts, each must be added to the policy, otherwise parts of the page stop working. That is why you should start in report-only mode (see below). The MDN CSP guide is a good reference.

A recommended starter set

Header Example value Risk of breakage
X-Content-Type-Options nosniff Very low
X-Frame-Options SAMEORIGIN Low (affects you if your site is embedded elsewhere)
Referrer-Policy strict-origin-when-cross-origin Low (some analytics tools lose referrer detail)
Permissions-Policy camera=(), microphone=(), geolocation=() Low (remove any feature your site uses)
Content-Security-Policy Start with Report-Only High (must be tested)

How to add them on Apache and Nginx

Apache / LiteSpeed (.htaccess). If mod_headers is enabled, add this to the .htaccess in your site root:

Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"

Nginx. Add to the relevant server block, test the configuration, then reload:

add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
nginx -t && systemctl reload nginx

For syntax details see the Apache mod_headers and Nginx headers module documentation. On shared hosting without server access, the .htaccess route is usually enough; if you cannot edit it, our support team can help.

Rolling out CSP safely

Instead of enforcing CSP straight away, publish it first as Content-Security-Policy-Report-Only. The browser does not block violations; it only logs them in the developer console. A starting point:

Content-Security-Policy-Report-Only: default-src 'self'; img-src 'self' data: https:; frame-ancestors 'self'

Open the developer console (F12), browse your pages and review the "violates the Content Security Policy" messages. After adding the legitimate sources to the policy, you can publish it without Report-Only. If your site uses many inline scripts (common with WordPress plugins), this stage can take a while.

Common mistakes

  • Enabling HSTS before HTTPS works fully: mixed content and certificate problems then reach visitors as error pages. Fix mixed content errors first.
  • Adding the same header in both the server and a plugin: the response gets duplicate headers, which browsers may treat as invalid. Check the current state with curl -sI first.
  • Enforcing CSP without testing: payment forms, maps or live chat widgets may suddenly stop working.
  • Not testing behind a cache: a CDN or cache layer can alter headers, so test against the real domain.

Are security headers enough on their own?

No. Headers are only one defensive layer. Combine them with up-to-date software, strong passwords, regular backups and a web application firewall (WAF). If you manage the server yourself, the Linux server hardening guide is a good complement, and if you suspect an incident, follow hacked website recovery. The OWASP Secure Headers Project is another up-to-date reference.

Conclusion

Security headers are a low-cost, reversible improvement. Start with nosniff, X-Frame-Options, Referrer-Policy and Permissions-Policy, try CSP in report-only mode, and browse your site after every change. If you get stuck, open a support ticket and our team will help.

 

Frequently Asked Questions

Do security headers affect SEO?

No direct ranking effect is expected, but a badly configured header that breaks page loading can hurt indirectly. Always check your site after changes.

Do I still need the X-XSS-Protection header?

Modern browsers no longer use that old XSS filter; protection comes from CSP and proper output encoding. Leaving it on an existing site does no harm, but it is not required on a new setup.

How do I verify the headers are working?

List the response headers with curl -sI https://yourdomain.com, or open the Network tab in your browser's developer tools and inspect the response headers of the request.

Where do I add headers if I use a service like Cloudflare?

Adding them on the origin server is usually enough because the proxy passes them on. If you also add the same header in proxy rules, use only one place to avoid duplicates.


Powered by WISECP
Top