HTTP Security Headers Explained: HSTS, CSP and Safe Values for Nginx, Apache and Cloudflare

IM Host Editorial3 min read
HTTP security headers HSTS and CSP configuration

An SSL certificate encrypts the connection, but it does not tell the browser how strictly to protect your visitors. HTTP security headers do. A few lines in your server configuration can stop downgrade attacks, clickjacking, MIME sniffing and many script injection attacks. This guide explains the headers worth adding, safe starting values and how to roll them out on Nginx, Apache or Cloudflare without breaking your site.

The headers that matter

HeaderProtects againstSafe starting value
Strict-Transport-Security (HSTS)Downgrade to HTTP, cookie theft on insecure networksmax-age=31536000; includeSubDomains
Content-Security-Policy (CSP)Injected scripts (XSS), unwanted third-party codeStart in report-only mode (see below)
X-Content-Type-OptionsBrowsers guessing file types and running uploads as scriptsnosniff
X-Frame-Options / CSP frame-ancestorsClickjacking (your site loaded inside a hidden frame)SAMEORIGIN or frame-ancestors 'self'
Referrer-PolicyLeaking full URLs with private data to other sitesstrict-origin-when-cross-origin
Permissions-PolicyUnneeded access to camera, microphone, locationcamera=(), microphone=(), geolocation=()

The old X-XSS-Protection header is deprecated in modern browsers; rely on CSP instead.

HSTS: do it in steps

HSTS tells browsers to use HTTPS only for your domain for the time set in max-age. It is powerful and hard to undo, because browsers remember it.

  1. Make sure every page and subdomain works on HTTPS with a valid certificate. See free SSL with Certbot and fixing mixed content.
  2. Start with a short value, such as max-age=300, and check nothing breaks.
  3. Increase to max-age=31536000 (one year). Add includeSubDomains only when every subdomain, including mail and old tools, supports HTTPS.
  4. Consider the browser preload list (preload) only after this has run safely for a while; removal from the list takes a long time.

Content-Security-Policy without breaking the site

CSP lists the sources allowed to load scripts, styles, images, fonts and frames. A strict policy blocks injected code, but a wrong policy can break analytics, chat, payment widgets or your page builder. Roll it out like this:

  1. Begin with Content-Security-Policy-Report-Only, which reports violations without blocking.
  2. Start with a base policy, for example: default-src 'self'; img-src 'self' data: https:; script-src 'self' https://www.googletagmanager.com; style-src 'self' 'unsafe-inline'; frame-ancestors 'self'; object-src 'none'; base-uri 'self'
  3. Open the browser console on key pages (home, product, checkout, contact) and add the domains you really need.
  4. When the console is clean for a while, switch to the enforcing Content-Security-Policy header.

Even a partial policy helps: frame-ancestors, object-src 'none' and base-uri 'self' are low-risk wins.

How to add headers

Nginx

Inside the server block for HTTPS:

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
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;

Note: in Nginx, an add_header inside a location block replaces the ones from the server block. Test with nginx -t, then reload.

Apache / .htaccess

Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

This needs the mod_headers module, which is enabled on most cPanel and LiteSpeed hosts.

Cloudflare

If your site is behind Cloudflare, you can enable HSTS in the SSL/TLS settings and add other headers with response header transform rules. See setting up Cloudflare with your hosting.

Check your headers

  • Run curl -I https://yourdomain.com and read the response headers.
  • In browser DevTools, open the Network tab, click the main document and look at Response Headers.
  • Re-check after plugin updates, CDN changes or moving servers, because headers can be lost.

Headers are one layer

Security headers reduce browser-side attacks; they do not replace updates, strong passwords, a firewall and backups. On a VPS, start with SSH keys, UFW and Fail2ban. Every IM HOST plan includes free SSL, and you can upgrade to OV, EV or wildcard certificates when your business needs them. Need help adding headers safely? Contact our support team.

IM Host EditorialNovember 25, 2026All articles