How to disable nginx server header?

17.3.2024

nginx how to
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.

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
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.

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

curl -I localhost

curl req

And also check the server header using browser developer tools.

browser dev tools