# try_files
-「ngx_http_core_module.html#try_files」
Syntax: | try_files file … uri; |
try_files file … =code; | |
Default: | — |
Context: | server, location |
检查按顺序指定的文件(file)是否存在,并使用第一个找到的文件进行请求处理;处理在当前上下文中执行。 文件的路径是根据root和alias指令从file参数构造。 可以通过在file的末尾指定斜杠来检查目录的存在,例如,“$uri/”。 如果未找到任何文件,则会进行内部重定向到由最后一个参数中指定的uri中。 例如:
location /images/ { try_files $uri /images/default.gif; } location = /images/default.gif { expires 30s; }
最后一个参数也可以指向命名位置,如下面的示例所示:
location / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
从版本0.7.51开始,最后一个参数也可以是HTTP响应码:
location / { try_files $uri $uri/index.html $uri.html =404; }
访问某个地址,返回固定内容:
location /client/index.php { try_files /index.html /index.html; }
# error_page
指令「error_page」处理由nginx生成的错误。
默认情况下,无论http状态代码如何,nginx都将返回代理服务器返回的任何内容。所有error_page不会处理proxy_pass的返回。应该使用proxy_intercept_errors on指令,然后再配置error_page指令。
参考文献
Module ngx_http_core_module
nginx not serving my error_page