为什么nginx内部redirect不会发生

我很确定我错过了一件显而易见的事情,但是这让我疯狂,我需要其他的眼球。

我得到了一个插件的应用程序来源是像这样组织的:

/ app / plugins / foo / www / …对应于我的urlhttp://example.com/plugins/foo/ …

我将下面的代码片段作为nginxconfiguration:

location /plugins/foo { alias /app/plugins/foo/www; try_files $uri /index.php =404; } location ~* ^/plugins/foo/(.*\.php)$ { alias /app/plugins/foo/www/$1; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

它适用于以下url:

  • http://example.com/plugins/foo/style/style.css
  • http://example.com/plugins/foo/index.php
  • http://example.com/plugins/foo/bar.php

但只要我试图达到(前控制器模式):

  • http://example.com/plugins/foo/?id=5
  • http://example.com/plugins/foo/another/path

我得下载php文件。

根据日志,try_files似乎做预期的,而不是内部redirect到PHP处理程序,它服务器文件为静态:

 2017/01/24 13:29:36 [debug] 20803#0: *6 using configuration "/plugins/foo" 2017/01/24 13:29:36 [debug] 20803#0: *6 http cl:-1 max:1048576 2017/01/24 13:29:36 [debug] 20803#0: *6 rewrite phase: 3 2017/01/24 13:29:36 [debug] 20803#0: *6 post rewrite phase: 4 2017/01/24 13:29:36 [debug] 20803#0: *6 generic phase: 5 2017/01/24 13:29:36 [debug] 20803#0: *6 generic phase: 6 2017/01/24 13:29:36 [debug] 20803#0: *6 generic phase: 7 2017/01/24 13:29:36 [debug] 20803#0: *6 generic phase: 8 2017/01/24 13:29:36 [debug] 20803#0: *6 access phase: 9 2017/01/24 13:29:36 [debug] 20803#0: *6 access phase: 10 2017/01/24 13:29:36 [debug] 20803#0: *6 post access phase: 11 2017/01/24 13:29:36 [debug] 20803#0: *6 try files phase: 12 2017/01/24 13:29:36 [debug] 20803#0: *6 http script var: "/plugins/git/" 2017/01/24 13:29:36 [debug] 20803#0: *6 trying to use file: "/" "/app/plugins/foo/www/" 2017/01/24 13:29:36 [debug] 20803#0: *6 trying to use file: "/index.php" "/app/plugins/foo/www/index.php" 2017/01/24 13:29:36 [debug] 20803#0: *6 try file uri: "/plugins/foo/index.php" 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 13 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 14 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 15 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 16 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 17 2017/01/24 13:29:36 [debug] 20803#0: *6 content phase: 18 2017/01/24 13:29:36 [debug] 20803#0: *6 http filename: "/app/plugins/foo/www/index.php" 2017/01/24 13:29:36 [debug] 20803#0: *6 add cleanup: 000000000153FF70 2017/01/24 13:29:36 [debug] 20803#0: *6 http static fd: 13 2017/01/24 13:29:36 [debug] 20803#0: *6 http set discard body 2017/01/24 13:29:36 [debug] 20803#0: *6 xslt filter header 2017/01/24 13:29:36 [debug] 20803#0: *6 HTTP/1.1 200 OK 

所以问题是:在这种情况下如何实现我的前端控制器模式?

编辑

 location /plugins/foo { alias /app/plugins/foo/www; try_files $uri /plugins/foo/index.php?$args; } 

我被redirect到核心前端控制器,而不是插件:

 2017/01/25 13:45:48 [debug] 14086#0: *197 using configuration "/plugins/foo" ... 2017/01/25 13:45:48 [debug] 14086#0: *197 try files phase: 12 2017/01/25 13:45:48 [debug] 14086#0: *197 http script var: "/plugins/foo/" 2017/01/25 13:45:48 [debug] 14086#0: *197 trying to use file: "/" "/app/plugins/foo/www/" 2017/01/25 13:45:48 [debug] 14086#0: *197 http script copy: "/plugins/git/index.php?" 2017/01/25 13:45:48 [debug] 14086#0: *197 http script var: "group_id=101" 2017/01/25 13:45:48 [debug] 14086#0: *197 trying to use file: "/index.php?group_id=101" "/app/plugins/foo/www/index.php?group_id=101" ====> ??? 2017/01/25 13:45:48 [debug] 14086#0: *197 internal redirect: "/index.php?group_id=101" 2017/01/25 13:45:48 [debug] 14086#0: *197 rewrite phase: 1 ... 

为什么当/app/plugins/foo/www/index.php?group_id=101有效时redirect到/index.php?group_id=101

为了避免与try_files一起使用alias 长期存在的问题 ,你可以使用if代替( 注意使用限制 )。 另请注意,使用$request_filename而不是$document_root$fastcgi_script_name来获取别名path名。

我testing了这个例子:

 location ^~ /plugins/foo { alias /app/plugins/foo/www; if (!-f $request_filename) { rewrite ^ /plugins/foo/index.php last; } location ~ \.php$ { if (!-f $request_filename) { rewrite ^ /plugins/foo/index.php last; } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; } } 
  • ^~修饰符使得这个location块优先于同级别的其他location块(参见这个文档 )。
  • rewrite语句自动附加参数(请参阅此文档 )。
  • 嵌套的location块处理别名范围内的PHP文件。
  • 总是include fastcgi_params; 使用fastcgi_param 之前避免后者被包含文件无声覆盖。