How to disable nginx server header?

2024-03-17

feature image

I am running nginx on my own laptop and i am using Pop!_OS 22.04 LTS

pre-requisites

Install nginx-extras

If you have installed nginx with apt package manager, you have to install the following package to disable the server header.

Terminal window
 
sudo apt update && sudo apt install nginx-extras

Edit nginx configuration file

Open the nginx configuration file in your favorite text editor. The default location of the configuration file is /etc/nginx/nginx.conf . Add the following line to the http block.

You can also addd it to server and location block
nginx.conf
 
events {
}
http {
server {
listen 80;
root /usr/share/nginx/html;
more_clear_headers Server; # will remove server header
server_tokens off; # will remove version number
}
}

And then restart the nginx server.

Terminal window
 
sudo nginx -t # to check if the configuration file is correct
sudo nginx -s reload # to reload the configuration file

check the server header using curl

Terminal window
 
curl -I localhost

curl req

And also check the server header using browser developer tools.

browser dev tools