nginx或apache提供的Yii站点只提供主页面服务


Yii site served by nginx or apache only serves main page

EDIT ONE:我也用apache在Windows 7上测试过这一点,在那里也发生了完全相同的事情。这意味着问题主要在于将所有页面路由到主页。

编辑二:根据#yii IRC频道中的建议,我用一个工作项目中的简化文件替换了.htaccess文件的内容。然而,这并没有使问题发生任何变化。

编辑三:通过对代码的一些更改,我现在可以很好地处理这个问题了。答案如下

我一直在尝试导入一个用Yii框架编写的网站,我已经将其导入到我的工作站。我遇到了一个问题,试图加载任何页面都只能直接加载到主页。这意味着所有请求的路由基本上都与我加载根请求一样。访问日志或错误日志中没有弹出错误;在nginx的访问日志中,请求看起来如下,其中"example"是"localhost"、"127.0.0.1"或服务器的IP地址:

Accessing root ( http://example/ ): 
127.0.0.1 - - [08/May/2013:13:05:28 -0700] 
"GET / HTTP/1.1" 200 8436 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"
Accessing login ( http://example/site/login ):
127.0.0.1 - - [08/May/2013:13:07:01 -0700] 
"GET /site/login HTTP/1.1" 200 8427 "http://example/" "Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"
Accessing the same controller by http://example/index.php?r=site/login:
127.0.0.1 - - [08/May/2013:14:40:45 -0700] 
"GET /index.php?r=site/login HTTP/1.1" 200 8419 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"

考虑到我已经设置了另外两个运行良好的Yii项目,它们使用的服务器配置与我将在下面粘贴的配置类似,我认为这可能是我还不热衷于应用程序本身工作的东西。如果有人对我从其他网站导入的工作项目中可以分享、尝试或比较的其他内容有任何建议,我们将不胜感激:(

edit:在配置中切换到更接近我实际使用的配置。考虑到该项目在Windows上的Apache中运行时也存在同样的问题,这可能无关紧要

server {
    server_name  localhost;
    listen 80;
    keepalive_timeout 70;
    root /usr/www/[project name omitted]/public_html;
    client_max_body_size 4M;
    client_body_buffer_size 4M;
    client_header_buffer_size 4M;
    location / {
        try_files $uri $uri/ /index.php?$args;
        autoindex on;
    }
    location ^~ /data/ {
        expires 30d;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    #Disable logging for favicon
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    #Disable logging for robots.txt
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    location ~ /themes/'w+/views {
        deny all;
        access_log off;
        log_not_found off;
    }
    # Pass all .php files onto a php-fpm/php-fcgi server.
    location ~ '.php$ {
        fastcgi_split_path_info ^(.+'.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        try_files $uri =404;
        #fastcgi_intercept_errors on;
        #fastcgi_connect_timeout 20;
        fastcgi_send_timeout 20;
        fastcgi_read_timeout 600; # For xdebug to work alright
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

您应该从location /配置中删除此行:

index index.html $yii_bootstrap;

基本上,您告诉Nginx它应该始终使用$yii_bootstrap作为索引文件。因此,线路try_files从未被使用。

我能看到的两件事。一般来说,对于Yii应用程序,我的位置块设置如下。不是index.php文件前面的"~"和正斜杠。我还会将您的索引选项移到该位置块的之外,并移到服务器根块中。所以你的位置块应该如下

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

第二件事与php位置块有关。除非您运行多个php文件,否则您只需要指定对index.php的访问权限,而不需要允许访问任何面向web的php文件。

location ~ index.php$ {

这个位置块应该对你有用:

location ~ index.php$ {
    fastcgi_split_path_info ^(.+'.php)(.*)$;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /path/to/yii/app$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_intercept_errors        off;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60; 
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
} 

在联系了这个项目的原始开发人员后,我已经把一切都整理好了,他告诉了我需要做的一些更改。它们如下:

  • 更新如下代码实例:

    session_start();
    $_SESSION['allowdownload'] = true;
    session_write_close();
    
  • 有了这个:

    $session=new CHttpSession;
    $session->open();
    $session['allowdownload'] = true;
    $session->close();
    
  • 以及将protected/config/main.php中urlManager数组中"showScriptName"的设置更改为"true"而不是"false"。我希望这能在某个时候帮助其他人!