Home > Operation and Maintenance > Nginx > What is the method for nginx to prohibit running php scripts in a specified directory?

What is the method for nginx to prohibit running php scripts in a specified directory?

王林
Release: 2020-11-06 11:12:30
Original
2520 people have browsed it

The way nginx prohibits running php scripts in a specified directory is to directly match the location conditions and then prohibit permissions, such as [location ~* ^/uploads/.*\.(php|php5)${ deny all;}].

What is the method for nginx to prohibit running php scripts in a specified directory?

#Permission is prohibited after matching the location condition directly.

(Learning video recommendation: java course)

Add the following configuration in the server configuration section

If it is a single directory

location ~* ^/uploads/.*\.(php|php5)$ 
 
{  
 
deny all;
 
}
Copy after login

If there are multiple directories

location ~* ^/(attachments|uploads)/.*\.(php|php5)$ 
 
{ 
 
deny all; 
 
}
Copy after login

Note: This configuration file must be placed in front of the following configuration to take effect.

location ~ \.php$ {
 
fastcgi_pass 127.0.0.1:9000;
 
fastcgi_index index.php;
 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 
include fastcgi_params;
 
}
Copy after login

Complete configuration example

location ~ /mm/(data|uploads|templets)/*.(php)$ {
 
deny all;
 
}
 
location ~ .php$ {
 
try_files $uri /404.html;
 
fastcgi_pass 127.0.0.1:9000;
 
fastcgi_index index.php;
 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 
include fastcgi_params;
 
}
Copy after login

After the configuration is completed, Nginx needs to be restarted to take effect.

Related recommendations: php training

The above is the detailed content of What is the method for nginx to prohibit running php scripts in a specified directory?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template