禁止访问所有.开头的隐藏文件设置
location ~* /.* {
deny all;
}
禁止访问指定文件或目录
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
只允许访问index.php
#只允许入口文件index.php访问
location ~ ^/index\.php$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-73.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
#只允许入口文件index.php和api.php访问
location ~ ^/(index\.php|api\.php)$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-73.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
然后在伪静态规则最后添加return 404;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
#只输入域名也能访问到默认的index.php,不至于被显示404
location ~ ^/$ {
break;
}
#访问以上情况之外的文件都显示404
return 404;
}
设置后,除了网站根目录下的index.php文件,其余的php文件都会显示404错误。