Skip to content

HTTP security headers for a safer website

HTTP security headers for a safer website

Website security and HTTP headers

In this post I go through how you can improve your website’s security by getting your server’s HTTP headers in order. HTTP headers should be set at server level, and a company that offers modern web hosting should take care of them for you without being asked. If you don’t have direct access to the server, you can also change the settings with WordPress plugins.

Mozilla offers a free, effective tool for testing your site before and after you change the settings, so you can see how much its security has improved. Test your site here: MDN HTTP Observatory.

1. Content Security Policy (CSP)

Content Security Policy is perhaps the most important single HTTP header. It helps you fend off Cross Site Scripting (XSS) attacks, which try to inject malicious code into your website. Pages where people log in are among the targets. After a successful attack, a hacker can get hold of the login details of the people signing in.

By default, the Content Security Policy header blocks traffic from outside servers. So it is important that your rules state exactly which external sources (such as Google Analytics) are allowed.

An example of a CSP setting in the configuration of an NGINX or Apache web server:

content-security-policy: script-src 'self' https://www.google-analytics.com

2. HTTP Strict Transport Security (HSTS)

Even if a website uses an HTTPS address and an SSL certificate, that doesn’t automatically mean complete security. It is important to check the HTTP headers so that the connection stays encrypted from start to finish. HTTP Strict Transport Security, or HSTS, forces all of the website’s traffic over a secure HTTPS connection.

With the HSTS header, you make sure your customers never end up, even by accident, doing things like online shopping over an insecure connection.

An example HSTS setting:

strict-transport-security: max-age=31536000; includeSubDomains; preload

3. X-Frame-Options

Clickjacking is a technique for tricking people into handing over their information while they browse websites that look safe. The attack often uses iFrame elements to “hijack” a site.

The modern way to prevent this is the CSP header from point 1 (its frame-ancestors directive), but X-Frame-Options is still a useful addition to protect older browsers.

To enable it on an Nginx web server:

add_header x-frame-options "SAMEORIGIN" always;

To enable it on an Apache web server:

header always set x-frame-options "SAMEORIGIN"

4. X-XSS-Protection (deprecated)

X-XSS-Protection, which used to be recommended, is now a deprecated header. Many modern browsers (such as Chrome and Edge) no longer use it by default, and if it is configured wrongly, it can even create vulnerabilities. Today the recommendation is to handle XSS protection primarily with a strong Content Security Policy (CSP) header (see point 1).

If you want to turn it off, or keep things compatible in a safe way, you can use the value “0”:

add_header x-xss-protection "0" always;

5. X-Content-Type-Options

MIME sniffing attacks exploit the way browsers guess file types and, at worst, can load malicious content onto a website. The X-Content-Type-Options header stops browsers from MIME sniffing and forces them to follow the file type the server declares.

To enable it on an Nginx web server:

add_header X-Content-Type-Options "nosniff" always;

To enable it on an Apache web server:

Header always set X-Content-Type-Options "nosniff"

Website security in general

Website security is the responsibility of everyone who runs a site. Reaching a good level of security can feel challenging, but the right HTTP headers are an easy and effective way to start. When the server is configured correctly, the security of the whole site improves considerably. If you use shared web hosting, check with your provider that they let you change these headers.

If you’d like me to check your site’s headers and other security settings for you, take a look at my security services.

Frequently asked questions

What are HTTP headers?

HTTP headers are metadata that travel with the traffic between the server and the browser. They tell the browser how the web page’s content should be handled, loaded and displayed. Correctly set security headers protect the site and its users from common attacks.

What is the most important HTTP security header?

Content Security Policy (CSP) is the most important single header today. It is a very effective way to prevent Cross-Site Scripting (XSS) attacks, because it strictly limits where the site can load scripts, images, styles and other content from.

How do I test my website’s security headers?

The easiest way is to use a free online tool such as the MDN HTTP Observatory. It scans your site, grades its current security and clearly lists which headers are missing or need adjusting.

Do I need the HSTS header if I already have an SSL certificate?

Yes, you do. An SSL certificate on its own makes encryption possible but doesn’t enforce it. The HSTS (HTTP Strict Transport Security) header forces the browser to always use a secure HTTPS connection and stops the connection from being switched to an unencrypted one (a so-called downgrade attack).

Blog

Read next

All articles
Google Tag Manager: what it is and how to use it

Guides

Google Tag Manager: what it is and how to use it

Google Tag Manager transforms the way digital marketing work gets done. In this guide I explain what GTM is, why every website needs it and how to set it up step by step, from installation all the way to the Consent Mode v2 settings.

How to stop spam on your WordPress site

Guides

How to stop spam on your WordPress site

Spam is one of the most common headaches for anyone who runs a WordPress site, but today it is easier to fight than ever. This guide covers the most effective ways to stop spam on your WordPress site, from good-quality web hosting to smart plugins and privacy-friendly CAPTCHA alternatives.

What is DNS?

Guides

What is DNS?

Without DNS, we would have to remember long strings of numbers to visit websites. This article explains in plain language what DNS is, how a lookup happens in milliseconds and why optimising your name server settings can make your browsing faster and safer.

Need a hand with this? Ask me.

Tuomas LindroosI answer myself, usually within two hours. You can also send a message through the contact form.