CakePHP在一个使用nginx的子目录中(重写规则?)

时间:2010-01-22 19:05:19

标签: cakephp nginx rewrite

我设法让这个工作回来了一段时间,但是在回到我开始的cakephp项目时,似乎我最近对nginx所做的任何改变(或者最近的更新)都破坏了我的重写规则。 / p>

目前我有:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        location /basic_cake/ {
            index  index.php;

            if (-f $request_filename) {
              break;
            }
            if (!-f $request_filename) {
              rewrite ^/basic_cake/(.+)$ /basic_cake/index.php?url=$1 last;
              break;
            }
        }

        location /cake_test/ {
            index  index.php;

            if (-f $request_filename) {
              break;
            }
            if (!-f $request_filename) {
              rewrite ^/cake_test/(.+)$ /cake_test/index.php?url=$1 last;
              break;
            }
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

    server {
        listen       8081;
        server_name  localhost;

        root /srv/http/html/xsp;

        location / {
            index  index.html index.htm index.aspx default.aspx;
        }

        location ~ \.(aspx|asmx|ashx|asax|ascx|soap|rem|axd|cs|config|dll)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

我遇到的问题是css和图像不会从webroot加载。相反,如果我访问http://localhost/basic_cake/css/cake.generic.css,我会得到一个页面告诉我:

  CakePHP:快速开发php   框架缺少控制器

     

错误:CssController不能   找到。

     

错误:创建类CssController   在文件下面:   应用程序/控制器/ css_controller.php

           

注意:如果要自定义此选项   错误消息,创建   应用程序/视图/错误/ missing_controller.ctp   CakePHP:快速开发php   框架

有人对如何解决这个问题有任何想法吗?

(我在ServerFault上发布了这个,但我有这种感觉,与此相比,没有多少人检查该网站,所以我可能不应该打扰......)

4 个答案:

答案 0 :(得分:6)

使用别名 try_files 指令可以更轻松地执行此操作。我已经开始使用PHP进行简单配置,并在服务器上的path / cakeproject中添加了一个Cake项目:

    root /var/www;
    index index.php;

    location /cakeproject {
            alias /var/www/cakeproject/app/webroot;
            try_files $uri $uri/ /cakeproject/app/webroot/index.php;
    }

    location ~ \.htaccess {
            deny all;
    }

    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }

蛋糕项目现在可以完美地从http://thedomain.com/cakeproject/

开始

答案 1 :(得分:5)

我设法通过将cake.base参数添加到cakephp配置而不是使用App.baseUrl(查看dispatcher.php中的代码)来解决这个问题。

所以假设我有/ var / www / html / cakeprj中的cakephp副本,我的WWWROOT是/ var / www / html:

  1. nginx主机配置

    # that's for all other content on the web host
    location / {
        root        /var/www/html;
        autoindex   off;
        index       index.php index.html index.htm;
    
        ...
    }
    
    # that's for cakephp
    location /cakeprj {
        rewrite     ^/cakeprj$ /cakeprj/ permanent;
        rewrite     ^/cakeprj/(.+)$ /$1 break;
        root        /var/www/html/cakeprj/app/webroot;
        try_files   $uri /$uri/ @cakephp;
    }
    
    # that's for all other php scripts on the web host
    location ~ \.php$ {
        root                                /var/www/html;
        fastcgi_pass                        unix:/var/lib/fcgi/php-fcgi.socket;
        ...
        include                             /etc/nginx/fastcgi_params;
    }
    
    
    # that's for cakephp execution
    location @cakephp {
        set $q $request_uri;
        if ($request_uri ~ "^/cakeprj(.+)$") {
            set $q $1;
        }
        fastcgi_param SCRIPT_FILENAME       /var/www/html/cakeprj/app/webroot/index.php;
        fastcgi_param QUERY_STRING          url=$q;
        fastcgi_pass                        unix:/var/lib/fcgi/php-fcgi.socket;
        include                             /etc/nginx/fastcgi_params;
    }
    
  2. app / config / core.php中的cakephp配置

    Configure::write('App.base', '/cakeprj');
    Configure::write('App.baseUrl', '/cakeprj/'); // seems like it doesn't matter anymore
    
  3. ...瞧 - 你得到正确的nginx服务cakephp静态文件,正确传递给cakephp调度程序的url以及正确生成url的cakephp。

    P.S。如果你的nginx不支持try_files我相信它的配置可以用if条件和另一个重写来重写。

答案 2 :(得分:3)

就我而言,我所做的是以下内容:

    location /cakeproj {
            rewrite_log on;
            error_log /var/log/nginx/notice.log notice; # just for debuggin

            if (-f $request_filename) {
                    break;
            }

            # Avoid recursivity
            if ($request_uri ~ /webroot/index.php) {
                    break;
            }

            rewrite ^/cakeproj$ /cakeproj/ permanent;
            rewrite ^/cakeproj/app/webroot/(.*) /cakeproj/app/webroot/index.php?url=$1 last;
            rewrite ^/cakeproj/(.*)$ /cakeproj/app/webroot/$1 last;
    }
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            # Edit listen directive in /etc/php5/fpm/pool.d/www.conf 
            fastcgi_index index.php;
            include fastcgi_params;
    }

它有效!!!

现在我遇到了缓慢,但我认为是PHP而不是与nginx相关的

干杯

答案 3 :(得分:0)

找到解决方案:

location /cakeprj {
        rewrite ^/cakeprj(.+)$ /cakeprj/app/webroot$1 break;
        try_files $uri $uri/ /cakeprj/index.php?$args;
}

其中cakeprj是cakephp目录。

参考:http://jamesmcdonald.id.au/it-tips/cakephp-in-a-subdirectory-nginx