【问题标题】:Nginx + Php-fpm + Zend Framework 1.11 strange rewrite errorNginx + Php-fpm + Zend Framework 1.11 奇怪的重写错误
【发布时间】:2012-02-02 01:06:58
【问题描述】:

我在 *nix 平台上使用 nginx 和 php5-fpm 有奇怪的重写问题。我使用 Router.php 类作为应用程序资源,并且该文件中有很多路由可以为相应的模块和控制器生成用户友好的 uri。现在,我想将一些基于正则表达式的应用程序路由移动到 nginx 级别,当我将路由移动到 nginx 配置时,我收到“指定的控制器无效”错误。

简单地说,当用户请求 /foo URI 时,我想触发 BarController 的 bazAction()。它在应用程序级别成功运行(通过路由器资源)

public function init() {
  $router = Zend_Controller_Front::getInstance()->getRouter();
  $f = new Zend_Controller_Router_Route_Static('foo',
                                                array( 'controller' => 'Bar',
                                                       'action' => 'baz'));
  $router->addRoute( 'foo', $f);
 ...

当我尝试使用像这样的 nginx 重写规则来执行此操作时

rewrite ^/foo /index.php/bar/baz?$args last;

我收到 Invalid controller specified (foo) 错误。我怎样才能正确处理这个重写规则?

我的 server.config 是:

    server {
        listen       80;
        server_name  www.dummy.com;
        root         /Users/foozy/Sites/dummy.com/public;
        index        index.php;

        location / {
            try_files $uri $uri/ @rewrites;
        }

        location @rewrites {
            rewrite ^/foo /index.php/bar/baz?$args last;
            # i also tried :
            # rewrite ^/foo /bar/baz?$args last;
            rewrite ^ /index.php?$args;
        }

        location ~ ^.+\.php {
          include /etc/nginx/fastcgi_params;
          fastcgi_pass 127.0.0.1:9000;
        }
    }

我的 fastcgi_params 文件是:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
... nginx's default params here ...
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
...
fastcgi_param           APPLICATION_ENV   development;
fastcgi_index           index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param           PATH_INFO         $fastcgi_path_info;

并且在 php.ini 中 fix_pathinfo 设置为 0:

cgi.fix_pathinfo=0

PHP v5.3.8、Nginx v1.0.10

【问题讨论】:

  • 检查这是否有效rewrite ^foo$ /index.php/bar/baz last;
  • @emaillenin 不,它不起作用。

标签: zend-framework url-rewriting nginx php


【解决方案1】:

【讨论】:

  • 简单来说,我想扼杀在代码中启动整个 foo/bar 的人。在示例中使用更具描述性的变量名称更有意义。尝试真正有意义的“userid / jakem”和“color / red”对。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 2013-09-28
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
相关资源
最近更新 更多