When running a website, security should always be one of your top priorities. One simple yet effective security measure is customising your web server headers. By default, Nginx displays its name and version in the server header, potentially exposing valuable information to attackers. In this comprehensive guide, we’ll explore how to customise Nginx server headers for better security without recompiling your web server, using the headers-more module. This technique helps protect your website by hiding sensitive server information and enhancing your overall security posture.
When you browse a website, the web server returns response headers that contain various pieces of information. By default, Nginx reveals its name and version in the “Server” header, which might look something like this:
Server: nginx/1.10.0
This information disclosure creates several security concerns:
By customising or removing these headers, you can implement an important security practice known as “security through obscurity” as one layer of your defence strategy.
Before getting started, ensure you have:
If you don’t already have Nginx installed, you can install it using your package manager:
apt-get update apt-get install nginx
The nginx-extras package contains the headers-more module along with several other useful modules. Install it using:
apt-get install nginx-extras
This installation provides the functionality we need without having to compile Nginx from source.
Open your main Nginx configuration file:
nano /etc/nginx/nginx.conf
There are several approaches you can take to modify your server headers, depending on your security requirements:
To keep the “nginx” name but hide the version number, add this line inside the http block:
http { server_tokens off; # Other existing configuration... }
To remove the Server header completely, add this inside the http block:
http { more_clear_headers Server; # Other existing configuration... }
To replace the default server name with your own custom name:
http { more_set_headers "Server: My Custom Server"; # Other existing configuration... }
For even more privacy, you can remove additional headers:
http { more_clear_headers Server; more_clear_headers Content-Type; more_clear_headers Accept-Ranges; more_clear_headers Content-Length; # Other existing configuration... }
Before restarting Nginx, verify that your configuration syntax is correct:
nginx -t
If successful, you should see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart the Nginx service to apply your changes:
service nginx restart
Or, on systems using systemd:
systemctl restart nginx
You can verify your header changes using the curl command:
curl -I your-website.com
This will display the headers returned by your server, allowing you to confirm that your customisations were applied successfully.
Let’s examine how different configurations affect your server headers:
HTTP/1.1 200 OK Server: nginx/1.10.0 Date: Mon, 13 May 2025 03:22:06 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive
HTTP/1.1 200 OK Server: nginx Date: Mon, 13 May 2025 03:26:12 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive
HTTP/1.1 200 OK Date: Mon, 13 May 2025 03:34:12 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive
HTTP/1.1 200 OK Date: Mon, 13 May 2025 03:38:28 GMT Connection: keep-alive
HTTP/1.1 200 OK Date: Mon, 13 May 2025 03:45:21 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Server: My Custom Server
When customising your Nginx headers, consider these best practices:
If you encounter issues when customising your headers, check the following:
Customising your Nginx server headers is a simple yet effective security measure that helps protect your website from potential attackers. By hiding or modifying server information, you reduce your attack surface and make it more difficult for malicious actors to identify vulnerabilities in your system. Combined with other security practices like regular updates, proper access controls, and security headers, customising your server headers contributes to a more robust security posture for your website.
Whether you choose to completely remove headers, set custom values, or simply hide version information, the headers-more module provides a flexible solution without requiring you to recompile Nginx. Start implementing these changes today to enhance your website’s security and protect your valuable data.