To configure Nginx to restrict access to PHP files based on file extensions, you can follow these steps:
Open the Nginx configuration file for your website or virtual host. This file is usually located in /etc/nginx/sites-available/
or /etc/nginx/conf.d/
.
Locate the server block or location block where you want to add the restriction. Inside the block, add the following code:
location ~ \.php$ {
deny all;
}
This code uses a regular expression to match any file ending with .php
and denies access to it.
Save the configuration file and exit the editor.
Test the Nginx configuration to make sure there are no syntax errors:
sudo nginx -t
sudo systemctl reload nginx
After following these steps, Nginx will deny access to any PHP file requested on your website or virtual host.