目前,我的Nginx服务器已设置为接受www.example.com/file
和www.example.com/file.php
加载file.php
。但是,因为Nginx设置如下所示:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
// More setup stuff
}
通过FastCGI将文件作为PHP处理的唯一方法是,如果URI以.php
结尾,否则,它只会下载文件。我如何设置它以便所有PHP文件都通过FastCGI处理,无论URI如何? (无论如何,我确信这样更安全,所以人们无法查看我的PHP代码。)
答案 0 :(得分:1)
虽然我认为通过单个php文件(即index.php)处理不以.php结尾的请求路径会更好,但你可以尝试这样的事情:
location / {
root /path/to/htdocs;
index index.php;
if (-e $request_filename.php) {
rewrite ^(.*)?$ /$1.php last;
break;
}
# rewrite requests without matching file index.php (request in: $_GET['req'])
if (!-e $request_filename) {
rewrite ^(.*)?$ /index.php?req=$1 last;
break;
}
}
location ~ \.php$ {
root /path/to/htdocs;
fastcgi_pass unix:/var/www/php-fpm/nginx.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
..
}