Home  >  Article  >  Backend Development  >  How to prevent nginx from running php

How to prevent nginx from running php

藏色散人
藏色散人Original
2020-10-29 10:28:462592browse

How to set up nginx to disable running php: first find the server configuration section; then add the configuration "location ~* ^/uploads/.*\.(php|php5)${deny all; in the server configuration section" }" will do.

How to prevent nginx from running php

Recommended: "PHP Video Tutorial"

It is prohibited to run PHP scripts in specified directories under Nginx

Nginx is simpler. It directly matches the location conditions and then prohibits permissions.

Add the following configuration in the server configuration section

If it is a single directory

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

If it is multiple directories

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

Note: This configuration file It 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;
 
}

*Finally give a 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;
 
}

After completing the configuration, remember to restart Nginx to take effect.

The above is the detailed content of How to prevent nginx from running php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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