How to configure Security Headers in Nginx and Apache
Last updated: November 10th 2022
Introduction
HTTP security headers are very important part of website security as it protect you against different types of attacks including, XSS, SQL injection, clickjacking, etc.
When you visit any website from your web browser, your browser requests it from the web server where the web site is hosted on. The web server then responds with HTTP Response Headers. These headers contains meta data, status error codes, cache rules and more. It also tell your browser how to behave when handling your website's content. When you use the web and interacts with websites, your browser stores its information. These headers will help you to outline communication and improve web security. There are six most important security headers that you should be aware of and we recommend implementing if possible.
Be careful, these headers are applying globaly in server if you want to change to specific files please add headers inside a location (nginx) or filesMatch (apache) block. So if you add headers inside any location, files will apply only headers inside location block and global headers will not works for these types of files.
1. HTTP Strict Transport Security (HSTS)
This header instructs a user agent to only use HTTPs connections and it also declared by Strict-Transport-Security. This will prevents web browsers from accessing web servers over non-HTTPS connections. Currently all major web browsers support HTTP strict transport security.
The Strict-Transport-Security header is ignored by the browser when your website is accessed over HTTP. This is because an attacker may intercept HTTP connections and inject the header or remove it.
You can implement HSTS in Apache by adding the following entry in /etc/apache2/sites-enabled/example.conf file:
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Save the file then restart the Apache service to apply the changes.
You can also implement HSTS in Nginx by adding the following entry in /etc/nginx/sites-enabled/example.conf file:
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
Save the file then restart Nginx to implement the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
2. Content Security Policy (CSP)
The Content-Security-Policy header is an improved version of the X-XSS-Protection header and provides an additional layer of security. It is very powerful header aims to prevent XSS and data injection attacks. CSP instruct browser to load allowed content to load on the website. All major browsers currently offer full or partial support for content security policy.
You can implement CSP in Apache by adding the following entry in /etc/apache2/sites-enabled/example.conf file:
Header always set Content-Security-Policy "default-src 'self'; font-src *;img-src * data:; script-src *; style-src *;"
Save the file then restart the Apache service to apply the changes.
You can also implement CSP in Nginx by adding the following entry in /etc/nginx/sites-enabled/example.conf file:
add_header Content-Security-Policy "default-src 'self'; font-src *;img-src * data:; script-src *; style-src *";
Save the file then restart Nginx to implement the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
3. X-XSS-Protection
X-XSS also known as Cross Site Scripting header is used to defend against Cross-Site Scripting attacks. XSS Filter is enabled by default in modern web browser such as, Chrome, IE, and Safari. This header stops pages from loading when they detect reflected cross-site scripting (XSS) attacks.
You can implement XSS protection using the three options depending on the specific need.
- X-XSS-Protection: 0 : This will disables the filter entirely.
- X-XSS-Protection: 1 : This will enables the filter but only sanitizes potentially malicious scripts.
- X-XSS-Protection: 1; mode=block : This will enables the filter and completely blocks the page.
To enable the X-XSS-Protection header in Apache, add the following line in your Apache web server default configuration file /etc/apache2/sites-enabled/example.conf:
Header set X-XSS-Protection "1; mode=block"
Next, restart the Apache service to apply the changes.
To enable the X-XSS-Protection header in Nginx, add the following line in your Nginx web server default configuration file /etc/nginx/nginx.conf:
add_header X-XSS-Protection "1; mode=block";
Next, restart the Nginx service to apply the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
4. X-Frame-Options
The X-Frame-Options header is used to defend your website from clickjacking attack by disabling iframes on your site. Currently it is supported by all major web browser. With this header, you tell the browser not to embed your web page in frame/iframe.
There are three ways to configure X-Frame-Options:
- DENY : This will disables iframe features completely.
- SAMEORIGIN : iframe can be used only by someone on the same origin.
- ALLOW-FROM : This will allows pages to be put in iframes only from specific URLs.
To enable the X-Frame-Options header in Apache, add the following line in your Apache web server default configuration file /etc/apache2/sites-enabled/example.conf:
Header always set X-Frame-Options "SAMEORIGIN"
Next, restart the Apache service to apply the changes.
To enable the X-Frame-Options header in Nginx, add the following line in your Nginx web server default configuration file /etc/nginx/sites-enabled/example.conf:
add_header X-Frame-Options "SAMEORIGIN";
Next, restart the Nginx service to apply the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block(Nginx) or Header set line in filesMatch block(Apache).
5. X-Content-Type-Options
The x-content-type header also called "Browser Sniffing Protection" to tell the browser to follow the MIME types indicated in the header. It is used to prevents web browser such as, Internet Explorer and Google Chrome from sniffing a response away from the declared Content-Type. nosniff header does not protect all sniffing-related vulnerabilities. Also there is no valid value for this header except nosniff.
To add the X-Frame-Options header in Apache, add the following line in your Apache web server default configuration file /etc/apache2/sites-enabled/webdock.conf:
Header always set X-Content-Type-Options "nosniff"
Next, restart the Apache service to apply the changes.
To add the X-Frame-Options header in Nginx, add the following line in your Nginx web server default configuration file /etc/nginx/sites-enabled/webdock
add_header X-Content-Type-Options nosniff;
Next, restart the Nginx service to apply the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
6. Referrer-Policy
The Referrer-Policy is a security header field that identifies the address of the webpage that requested the current webpage. By checking the referrer, the new webpage can see where the request originated. The Referrer-Policy can be configured to cause the browser to not inform the destination site any URL information.
To add the Referrer-Policy header in Apache, add the following line in your Apache web server default configuration file /etc/apache2/sites-enabled/webdock.conf:
Header always set Referrer-Policy "strict-origin"
Next, restart the Apache service to apply the changes.
To add the Referrer-Policy header in Nginx, add the following line in your Nginx web server default configuration file /etc/nginx/sites-enabled/example:
add_header Referrer-Policy "strict-origin";
Next, restart the Nginx service to apply the changes.
Note: If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
7. Permissions-Policy
The Permissions-Policy is a new header that allows site to control which APIs or features can be used in the browser.
To add the Permissions-Policy header in Apache, add the following line in your Apache web server default configuration file /etc/apache2/sites-enabled/webdock.conf:
Header always set Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()"
Next, restart the Apache service to apply the changes.
To add the Referrer-Policy header in Nginx, add the following line in your Nginx web server default configuration file /etc/nginx/sites-enabled/example:
add_header Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()";
Next, restart the Nginx service to apply the changes.
Note: Change permissions based on your website needs ,the values shown here are just an example policy with no basis in a real-life use-case. If you want to apply these headers to specific files, please add the add_header line in location block (Nginx) or Header set line in filesMatch block (Apache).
For the full list of current and proposed Permissions-Policy values which can be used in this directive, please click here.
Conclusion
We hope this article has provided some guidelines and helpful tips on setting the most common Security related headers. If you have any questions or comments, let us know in the comment section below!
Related articles
-
Server Security Checklist
In this article we list a number of things you should check if you are setting up a server from scratch as well as a few things which can enhance security on our Perfect Server Stacks.
Last updated: November 10th 2022
-
How to check for open ports on your Ubuntu server
This article details various approaches to finding out which ports are open and accessible on your server.
Last updated: November 10th 2022
-
How to work with your firewall (UFW - Uncomplicated Firewall)
In this article we show how UFW - or Uncomplicated Firewall - works along with common commands and usage examples.
Last updated: January 10th 2024
-
SSH Security Configuration Settings
This article lists various settings for the SSH Daemon which impact server security.
Last updated: February 1st 2024
-
How to configure Fail2Ban for common services
How fail2ban can be configured for common services as well as how to utilize the fail2ban CLI tools to check status of various jails, unbanning users and more.
Last updated: August 22nd 2023
-
How to Secure Nginx with Naxsi Firewall on Ubuntu 18.04 VPS
This Article describes how you can set up and configure Naxsi firewall on a Webdock LEMP stack on Ubuntu Bionic 18.04.
Last updated: November 10th 2022
-
How to Secure Nginx with Naxsi Firewall on Ubuntu 20.04 VPS
This Article describes how you can set up and configure Naxsi firewall on a Webdock LEMP stack on Ubuntu Focal 20.04.
Last updated: March 8th 2024
-
How to enable Encryption for MariaDB
Enable Encryption of your database data with MariaDB as well as force all new tables created to be encrypted.
Last updated: October 29th 2024
-
How to Scan Your Webdock Server for Malware and Virus
This guide provides basic step-by-step instructions to install various tools to scan your server for malware and viruses.
Last updated: July 19th 2023
-
How To Use Our Free BotGuard Bot Protection
In this article we show you how to activate and use our Free BotGuard Bot Protection which is included for free with all our VPS servers.
Last updated: November 4th 2024
-
Enhancing Nginx Security with IP Filtering and Password
A guide to enhance Nginx security with IP filtering (specific IP, and, IP ranges) and Password
Last updated: November 25th 2023
-
Securing Ubuntu: How to Detect System Vulnerabilities
Detect system vulnerabilities using Vuls
Last updated: December 20th 2023
-
Secure VPS Communication with SSL and UFW
A detailed guide to securely your communicate with your servers without requiring a VLAN setup.
Last updated: March 4th 2024
-
Configuring UFW and Fail2Ban to Mitigate Basic DDos Attacks
Instructions to protect your server from basic DDos attacks using UFW and Fail2Ban
Last updated: May 28th 2024