nginxconfigurationtry_file与条件重写基于目标文件是否存在

我正在使用如下所示的设置将所有未发现的文件redirect到单个回退位置

location / { try_files $uri @missing; } location @missing { rewrite ^(.*)$ /fallback.php last; } 

这很好,但我现在有一个更复杂的要求,我有效地需要回退到我的后备。

所以 – 我需要扩展这个,所以如果fallback.php不存在,它会重写为fallback2.php

更新:

这里是我的configuration文件的其余部分用@AD7six在注释中build议的try_files行写的,以防万一PHP处理程序出现问题:

 server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name ~^(?<subdomain>\w*?)?\.?(?<domain>\w+\.\w+)$; if ($subdomain = "") { set $subdomain "www"; } if (!-d "/home/me/projects/$domain/$subdomain") { set $subdomain "www"; } root /home/me/projects/$domain/$subdomain; index index.php index.html index.htm; location / { try_files $uri /fallback.php /fallback2.php; } location ~ \.php$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

如果我只在try_files行有一个位置它的工作原理:

 try_files $uri /fallback.php; 

但当我第二个位置统计下载fallback.php的HTML而不是redirect

 try_files $uri /fallback.php /fallback2.php; 

 location = /fallback.php { if (!-e $request_filename) { [...] } } 

注意:如果你有* .php的regexped-locations,他们将会优先。