我正在使用 YII 框架,在 Apache 服务器上一切正常,但在 Nginx 上却出错了。当我请求 URL www.test.com/index.php/a/b 时,$_SERVER['SCRIPT_NAME'] 返回 '/index.php' 这正是我需要的,但是当我请求 URL www.test 时。 com/a/b 被重写为同一个名为 'index.php' 的文件,$_SERVER['SCRIPT_NAME'] 变成了与 Apache 不同的 'index.php'。我的 nginx 配置如下:
server {
set $host_path "/data/yii/application";
access_log off;
server_name www.yii.com;
root $host_path;
set $yii_bootstrap "index.php";
charset utf-8;
location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ $yii_bootstrap?$args;
}
location ~ ^/(protected|framework|themes/\w+/views) {
deny all;
}
#avoid processing of calls to unexisting static files by yii
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
location ~ /\.ht {
deny all;
}
}