遵循陷阱指南 我正在尝试使用 try_files 指令清理我的 URL 重写。但是,当我在安装的 Nginx 0.8.54-4(来自 dotdeb)上使用此指令时,我尝试访问该站点(其中包含 index.php PHP 脚本的源代码)时会收到下载 BIN 文件的提示。那么这些指令中的任何一个是否与我的 nginx 版本不兼容,或者这个虚拟主机有什么问题?
server {
listen 80;
server_name site.loc;
root /home/user/www/site.loc/docroot;
index index.html index.php;
location / {
try_files $uri $uri/ @runphp;
}
# Pass all .php files to PHP-FastCGI
location @runphp {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
另外,这是最好的方法吗?我原来的(工作)配置如下所示:
server {
listen 80;
server_name site.loc;
root /home/user/www/site.loc/docroot;
index index.html index.php;
# Route all requests for non-existent files to index.php
if (!-e $request_filename) {
rewrite ^ /index.php last;
}
# Pass PHP scripts to php-fastcgi listening on port 9000
location ~ \.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
更新:以下重写工作正常 - 我必须使用 wget 进行测试,因为浏览器会缓存响应。我还更新到了 Nginx 1.0.5(Ubuntu 11.10 中的默认版本)。
server {
listen 80;
server_name site.loc;
root /home/user/www/site.loc;
index index.html index.php;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
# Route all requests for non-existent files to index.php
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# Pass PHP scripts to php-fastcgi listening on port 9000
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on
# this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi
# on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
答案1
你的配置有误。由于 PHP 无法解析不存在的 fallback URI @runphp
,所以你收到下载提示。
尝试这样的操作:
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}