nginx中的多层次的catch-all index.php

我有以下的nginxconfiguration:

server { listen 80 default_server; root /var/www; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires 24h; log_not_found off; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } } 

当任何URL被访问时,这通过/var/www/index.php传递一切。

然而,我想要在任意的第一级子目录中的index.php文件覆盖根index.php文件。 即

http://example.com/ – > /var/www/index.php
http://example.com/test – > /var/www/index.php
http://example.com/test/testing – > /var/www/index.php

但是如果 /var/www/test/index.php存在:

http://example.com/ – > /var/www/index.php(不变)
http://example.com/test – > /var/www/test/index.php
http://example.com/test/testing – > /var/www/test/index.php

我已经尝试了一些不同的正则expression式,但是很难过。 有任何想法吗? 在Apache中,这对于.htaccess来说是微不足道的,但是这显然不是一个选项。

 location / { if (-e $request_filename) { break; } rewrite (.*/)([^\/]+) $1 last; try_files $uri $uri/; } 

更改最后 redirect为外部redirect,而不是内部重写。

基于h0tw1r3的解决scheme,但没有if:

 location / { try_files $uri $uri/ $uri/index.php?$args @pop; } location @pop { rewrite (.*/)([^\/]+/?) $1 last; }