状态码在nginx的try_files指令中

是否有可能使用当前状态代码作为try_files的参数?

例如,我们尝试提供特定于主机的503静态响应,或者如果找不到,则为全服务器回退:

 error_page 503 @error503; location @error503 { root /path_to_static_root/; try_files /$host/503.html /503.html =503; } 

有一些这些指令,所以这样做会很方便:

 error_page 404 @error error_page 500 @error error_page 503 @error location @error { root /path_to_static_root/; try_files /$host/$status.html /$status.html =$status; } 

但是, variables文档没有列出我们可以用来做这件事的任何东西。

有没有可能,还是有其他的方法来做到这一点?

你的例子有无限循环。

可能你想要这样的东西:

 error_page 404 @error404; error_page 500 @error500; error_page 503 @error503; location @error404 { root /path_to_static_root/; try_files /$host/404.html /404.html =404; } location @error500 { root /path_to_static_root/; try_files /$host/500.html /500.html =500; } location @error503 { root /path_to_static_root/; try_files /$host/503.html /503.html =503; } 

..它会更快,然后与variables。

NGINX常见问题解答: 有没有一种合适的方式来使用nginxvariables来缩短configuration的各个部分,将它们作为macros来configuration模板的一部分工作?

更新:

社区wiki不是文档。 从官方文档:

$status – 响应状态(1.3.2,1.2.2)

@ http://nginx.org/en/docs/http/ngx_http_core_module.html#variables