How to Deny Access to All Files in NGINX


deny access to all files in nginx

NGINX is a powerful web server that provides many useful features. It allows you to control access to URLs, directories and even files. Let us take a look at how to deny access to all files in NGINX.

 

How to Deny Access to All Files in NGINX

Here are the steps to deny access to all files in NGINX.

 

1. Open NGINX config file

Open NGINX config file in a text editor. It is generally located at /etc/nginx/nginx.conf. If you have set it up at a different location, then update the path in the command below


$ sudo vim /etc/nginx/nginx.conf

 

2. Deny access to files

Let’s say you want to deny access to all php files on your website. In that case, add the following block,

location ~\.php$ {
deny all;
}

 

If you want to deny access to all files in a specific folder (e.g /admin/)


location ~ /admin/.*$ {
allow 192.168.0.0/24;
deny all;
}

 

If you want to deny access to all except a few specific IPs, you can mention them using the allow directive

location ~\.php$ {
 deny all;
 allow 54.4.1.13;
 allow 54.21.11.10;
}

 

You can also use the CIDR notation in allow directive to specify a range of IPs


location ~\.php$ {
 deny all;
 allow 54.34.21.0/24;
}

 

3. Reload NGINX Server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!