π Securing & Customizing Nginx Server Headers
π Hide the Nginx Version Numberβ
β¨ Steps:β
- Open the Nginx configuration file:
sudo vi /etc/nginx/nginx.conf
- Inside the
http
block, add the following line:server_tokens off;
- Restart Nginx to apply changes:
sudo systemctl restart nginx
π Hide or Change the Server Nameβ
β¨ Steps:β
- Install the required package:
sudo apt-get install nginx-extras
- Open your Nginx site configuration file:
vi /etc/nginx/sites-available/your-site.conf
- Inside the
server
block, add this line:more_set_headers 'Server: abc';
- Restart Nginx:
sudo systemctl restart nginx
π Verify the Changesβ
Run the following command to check the server headers:
curl -I http://3.108.252.119/
Example Output:
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2024 06:27:18 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 11 Dec 2024 06:07:32 GMT
Connection: keep-alive
ETag: "67592c24-267"
Server: abc
Accept-Ranges: bytes
π Restrict Access to Specific IPsβ
β¨ Steps:β
- Open the site configuration file:
sudo vi /etc/nginx/sites-available/default
- Add the following lines inside the
server
block:allow 59.163.196.138;
allow 65.234.321.135;
deny all; - Restart Nginx:
sudo systemctl restart nginx
π« Block a Specific IPβ
β¨ Steps:β
- Open the configuration file:
sudo vi /etc/nginx/sites-available/default
- Add the following lines inside the
server
block:deny 67.234.543.22;
allow all; - Restart Nginx:
sudo systemctl restart nginx
π Password Protect Your Websiteβ
β¨ Steps:β
- Install the apache2-utils package:
sudo apt-get install apache2-utils
- Create a password file for authentication:
(This will prompt you to set a password for
sudo htpasswd -c /etc/nginx/.htpasswd yourusername
yourusername
) - Open your siteβs configuration file:
vi /etc/nginx/sites-available/website-url.conf
- Add the following lines inside the
server
block:auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd; - Restart Nginx:
sudo systemctl restart nginx
if you can not change in the www- fastcgi_param PHP_ADMIN_VALUE "open_basedir=/var/www/html/screening-round/backend/:/usr/lib/php/:/tmp/";
π Now your Nginx server is more secure and customized! π