2012-07-10 147 views
1

我正在使用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']变成'index.php',它与Apache不同。我的nginx的conf是如下:

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; 
    } 
} 
+0

因为nginx做不同的事情。也许你应该报告一个错误。 – 2012-07-10 04:53:33

+0

@Ignacio Vazquez-Abrams 它的配置不同。它确实如何配置它。 – VBart 2012-07-10 20:01:33

回答

2

这部分是错误的:

set $yii_bootstrap "index.php"; 

location/{ 
    index index.html $yii_bootstrap; 
    try_files $uri $uri/ $yii_bootstrap?$args; 
} 

它应该是:

location/{ 
    index index.html index.php; 
    try_files $uri $uri/ /index.php$uri?$args; 
} 

参见:http://nginx.org/en/docs/faq/variables_in_config.html

-

而且S部:

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 ~ ^(?<script>.+\.php)(?<pathinfo>.*)$ { 
    try_files $script =404; 

    fastcgi_pass 127.0.0.1:9001; 
    include fastcgi_params; 
    fastcgi_param SCRIPT_FILENAME $document_root$script; 

    #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI 
    fastcgi_param PATH_INFO  $pathinfo; 
    fastcgi_param PATH_TRANSLATED $document_root$script; 
} 

-

而且你应该避免这样的事情:

set $host_path "/data/yii/application"; 
root $host_path; 

必须是:

root /data/yii/application; 

http://nginx.org/en/docs/faq/variables_in_config.html

相关问题