简体   繁体   English

由Nginx或Apache服务的Yii网站仅提供首页

[英]Yii site served by nginx or apache only serves main page

EDIT ONE: I've also tested this on Windows 7 with apache, and there the exact same thing happens. 编辑之一:我也在Windows 7上使用apache测试了它,并且发生了完全相同的事情。 This means that the problem is primarily the routing of all pages to the home page. 这意味着问题主要出在所有页面到主页的路由上。

EDIT TWO: As per a suggestion in the #yii IRC channel, I replaced the contents of the .htaccess file with a much-simplified one from a working project. 编辑二:根据#yii IRC频道中的建议,我用一个正在工作的项目中的一个简化得很简单的文件替换了.htaccess文件的内容。 This hasn't yielded any change in the problem, however. 但是,这并没有在问题上产生任何变化。

EDIT THREE: I've got this working just fine now with some changes to my code. 编辑三:通过对代码进行一些更改,我现在可以正常工作了。 Answer below 在下面回答

I've been trying to import a site written with the Yii Framework that I've imported to my workstation. 我一直在尝试导入一个使用Yii Framework编写的网站,该网站已导入到我的工作站。 I'm encountering a problem where trying to load any page does nothing but direct to the main page. 我遇到一个问题,尝试加载任何页面除了直接指向主页外什么都不做。 This means all requests are essentially routed exactly as if I loaded the root request. 这意味着所有请求基本上都像我加载根请求一样完全路由。 No errors pop up in the access log or error log; 在访问日志或错误日志中不会弹出错误; the requests look like the following in nginx's access log, where 'example' is either 'localhost', '127.0.0.1', or the server's IP address: 在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"

Considering I've got 2 other Yii projects set up which work fine, which are using a server configuration similar to which I'll also paste below, I'm thinking that it may be something in the workings of the application itself that I'm not keen to yet. 考虑到我还有另外两个Yii项目可以正常工作,它们使用的服务器配置与我还将在下面粘贴的服务器配置相似,我认为这可能是应用程序本身的工作原因我还不热衷。 If anyone has any suggestions for anything else I could share, try out, or compare to in the working projects I've imported from other sites, it'd be greatly appreciated :) 如果有人对我可以分享,尝试或与我从其他站点导入的工作项目进行比较,有任何建议,将不胜感激:)

edit: switched in config closer to what I had actually been using. 编辑:切换到更接近我实际使用的配置。 This is probably irrelevant considering the project has the same problem while running in Apache on Windows. 考虑到该项目在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;
    }
}

You should remove this line from your location / configuration: 您应该从您的location /配置中删除此行:

index index.html $yii_bootstrap;

Basically you tell Nginx that it should always use $yii_bootstrap as index file. 基本上,您告诉Nginx它应该始终使用$yii_bootstrap作为索引文件。 So the line try_files is never used. 因此,从未使用try_files行。

Two things that I can see. 我可以看到两件事。 Generally for Yii apps I have my location block setup as follows. 通常对于Yii应用程序,我的位置阻止设置如下。 Not the "~" and the forward slash before your index.php file. 不是index.php文件前的“〜”和正斜杠。 I'd also move your index option outside of that location block and into the server root block. 我还将您的索引选项移到该位置块之外 ,并移入服务器根块。 So your location block should look as follows 因此您的位置块应如下所示

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

The second thing has to do with your php location block. 第二件事与您的php位置块有关。 Unless you're running multiple php files, you only need to specify access to index.php rather than allow access to any web facing php file. 除非您正在运行多个php文件,否则只需指定对index.php的访问权限,而不是允许访问任何面向Web的php文件。

location ~ index.php$ {

This location block should work for you though: 但是,此位置块应为您工作:

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

I've got everything sorted out after contacting the original developer of this project, who has informed me of a few changes that needed to be made. 与该项目的原始开发人员联系后,我已经整理了一切,后者已通知我需要进行的一些更改。 They are as follows: 它们如下:

  • Updating instances of code that looks like this: 更新如下代码的实例:

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

     $session=new CHttpSession; $session->open(); $session['allowdownload'] = true; $session->close(); 
  • As well as changing the setting in protected/config/main.php for 'showScriptName' in the urlManager array to 'true' rather than 'false'. 以及将urlManager数组中protected / config / main.php中“ showScriptName”的设置更改为“ true”而不是“ false”。 I hope that this helps someone else out at some point! 我希望这可以在某些时候帮助别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM