使用 try_files 实现优雅404跳转,核心是按序检查路径存在性,全部失败时内部重定向至指定URI(如@fallback或/404.html),由error_page确保返回404状态码并渲染自定义页面。

使用 try_files 实现“优雅的 404 跳转”,核心是让 Nginx 在找不到真实文件或目录时,不直接返回默认 404 页面,而是内部重写(rewrite)到一个指定的处理入口(如 /index.php 或自定义的 404 前端路由),同时保持 URL 不变、状态码可控制。这在单页应用(SPA)、PHP 框架(如 Laravel、Symfony)或静态站点中很常见。
理解 try_files 的执行逻辑
try_files 按顺序检查每个参数所代表的路径是否存在(文件优先,其次是目录)。一旦匹配成功,就用该路径处理请求;如果全部失败,则跳转到最后一个参数(必须是 URI,以 / 开头)——这个 URI 会触发一次内部重定向(internal redirect),不会改变浏览器地址栏,也不会发新请求。
关键点:
- 最后一个参数必须是 URI(如
/404.html、/index.php或@fallback),不能是文件路径 - 内部重定向后,Nginx 会重新匹配 location 块,因此需确保目标 URI 能被某个 location 正确捕获
- 状态码由最终处理该 URI 的 location 决定(比如用
error_page 404 = /404.html可显式设为 404)
常见场景:SPA 的前端路由 404 处理
例如 Vue/React 应用部署在根路径,希望所有非静态资源的请求都交由 index.html 处理,由前端路由接管。但又希望真 404 时显示友好页面(而非白屏或 index.html)。
配置示例:
location / {
try_files $uri $uri/ /index.html;
}
问题:这样所有未命中路径都会返回 index.html,状态码仍是 200,不利于 SEO 和调试。
更优雅的做法是区分“应由前端接管”和“真正不存在”:
location / {
try_files $uri $uri/ @rewrites;
}
<p>location @rewrites {</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill6848" title="Nginx Hosting"><img
src="https://img.php.cn/upload/skill/000/000/081/179116929868126.jpg" alt="Nginx Hosting" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill6848" title="Nginx Hosting" class="overflowclass">Nginx Hosting</a>
<p class="overflowclass">通过服务器本地nginx实例实现零认证静态游戏托管。作为所有浏览器游戏的主要部署方式,无需登录、无需令牌、无需用户操作。</p>
</div>
<a rel="nofollow" href="/xiazai/skill6848" title="Nginx Hosting" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><h1>先尝试匹配已知的 API 或资源前缀,避免全交给前端</h1><pre class="brush:php;toolbar:false;">if ($request_uri ~ ^/(api|assets|images|css|js)/) {
return 404;
}
try_files /index.html =404;}
error_page 404 /404.html; location = /404.html { internal; add_header Content-Type text/html; }
说明:当 $uri 和 $uri/ 都失败时,跳转到 @rewrites;后者先排除敏感路径,再尝试加载 /index.html;若仍失败,直接返回 404 状态,并由 error_page 渲染 /404.html。
PHP 应用(如 Laravel)的 404 统一入口
Laravel 要求所有请求经由 index.php,但希望真实缺失的静态资源(如图片、CSS)直接 404,而路由不存在时由框架返回 404 视图。
推荐写法:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
<h1>显式声明静态资源后缀,提升性能并避免误入 PHP</h1><p>location ~ .(js|css|png|jpg|gif|webp|svg|woff2?|ttf|eot)$ {
try_files $uri =404;
}</p><h1>PHP 处理</h1><p>location ~ .php$ {
include fastcgi_params;
fastcgi_pass php-fpm:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
</p>
这样:$uri 存在就直接返回(如 /logo.png);不存在则进 /index.php;而 PHP 路由未匹配时,Laravel 自身会返回 404 响应(含正确状态码和视图)。
自定义 404 页面并保留状态码
如果只想用 try_files 触发自定义 404 页面,且确保返回 404 状态码(不是 200),必须配合 error_page:
location / {
try_files $uri $uri/ @notfound;
}
<p>location @notfound {
error_page 404 /custom-404.html;
return 404;
}</p><p>location = /custom-404.html {
internal;
add_header Content-Type text/html;
}</p>
注意:return 404 会立即返回 404 响应,然后由 error_page 404 捕获并内部重定向到 /custom-404.html,最终响应状态码仍是 404,内容为你指定的 HTML。










