简体   繁体   中英

Nginx - block all *.php files except index

I'm new to nginx. I need to block all .php files for security reasons. Any simple way to do it simmilar to Apache's .htaccess, as below:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule !^index.php index.php [L,NC]

Thanks in advance!

Instead of usual nginx PHP handler location

location ~ \.php$ {
    ... # FastCGI PHP handler here
}

use something like

location ~ \.php$ {
    rewrite ^ /index.php last;
}
location = /index.php {
    ... # FastCGI PHP handler here
}

Although regex matching locations ( location ~ <regex> { ... } ) are taking priority over the prefix locations ( location <URI prefix> { ... } ) on the request URI match, exact matching location ( location = <URI> { ... } ) are taking priority over both of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM