【问题标题】:CakePHP in a subdirectory using nginx (Rewrite rules?)CakePHP 在使用 nginx 的子目录中(重写规则?)
【发布时间】:2011-01-08 08:24:21
【问题描述】:

我设法让它工作了一段时间,但回到我开始的 cakephp 项目时,似乎我最近对 ​​nginx 所做的任何更改(或者可能是最近的更新)都违反了我的重写规则。

目前我有:

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 上发布了这个,但我觉得与这个相比,没有多少人检查那个站点,所以我可能不应该打扰......)

【问题讨论】:

    标签: cakephp nginx rewrite


    【解决方案1】:

    使用 aliastry_files 指令有一种更简单的方法。我从一个使用 PHP 的简单配置开始,并在服务器上的路径 /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/完美运行

    【讨论】:

    • 部分工作;不知何故,我的 ajax 搜索通过这种方法被错误的结果破坏了。将检查是什么问题,然后再回来。
    • 我不使用 Cake,我正在设置一些名为 ClusterControl 的软件,它使用基于 Cake 的 UI。使用这种方法没有问题,也许您有项目特定的问题?
    【解决方案2】:

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

    假设我的 cakephp 副本位于 /var/www/html/cakeprj 并且我的 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
      

    ...瞧——你得到了正确地为 cakephp 静态文件提供服务的 nginx,正确地传递给 cakephp 调度程序的 url,并且 cakephp 也正确地生成了 url。

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

    【讨论】:

    • 您对使用这种方法设置多个蛋糕应用程序有什么建议吗?我有大约 20 个蛋糕应用程序,它们目前都在使用包含您建议的配置的变体,但如果可能的话,我想消除这些包含。 (应用程序可以通过简单的模式 appA-.* appB-.* appC-.* 匹配)
    【解决方案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 相关

    干杯

    【讨论】:

    • 作为一个新手 nginx 构建器,我比上面使用 '@cakephp' 的构建器更幸运;我忽略了 php.ini 的考虑
    【解决方案4】:

    找到解决办法:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 2017-11-27
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多