fix: nginx SPA路由移除$uri/,避免/assets等路径被当成静态目录返回403
Deploy / Staging E2E Tests (push) Has been cancelled
Deploy / Build Production Runtime Images (push) Has been skipped
Deploy / Deploy Production (push) Has been skipped
Deploy / Production Browser E2E (push) Has been skipped
Deploy / Deploy Staging (push) Failing after 115h22m58s
CI/CD Pipeline / Frontend Lint (push) Failing after 115h23m32s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 115h23m32s

问题根因:
- try_files $uri $uri/ /index.html 中的 $uri/ 会匹配与构建产物目录同名的路由
- Vite 默认将静态资源放在 assets/ 目录下
- 访问 /assets 时,$uri/ 匹配到 assets/ 目录,Nginx 尝试目录访问返回 403
- 类似的路径冲突还有 /dashboard /history 等如果有同名目录也会出问题

修复方案:
- 移除 $uri/,改为 try_files $uri /index.html
- 真实存在的静态文件(如 /assets/index-abc.js)仍然能通过 $uri 正确返回
- 前端路由路径统一 fallback 到 index.html 由 SPA 路由处理

影响范围:nginx.conf / nginx-production.conf / nginx-staging.conf 三个文件
This commit is contained in:
xiaoxia
2026-07-04 17:30:12 +08:00
parent 254ffd5391
commit efb9d6c57f
3 changed files with 6 additions and 3 deletions
+2 -1
View File
@@ -13,8 +13,9 @@ server {
client_max_body_size 800m;
# SPA routing - all routes to index.html
# 注意:不能加 $uri/,否则 /assets 等与构建产物目录同名的路由会被当成目录访问,返回 403
location / {
try_files $uri $uri/ /index.html;
try_files $uri /index.html;
}
# API proxy
Regular → Executable
+2 -1
View File
@@ -13,8 +13,9 @@ server {
client_max_body_size 800m;
# SPA routing - all routes to index.html
# 注意:不能加 $uri/,否则 /assets 等与构建产物目录同名的路由会被当成目录访问,返回 403
location / {
try_files $uri $uri/ /index.html;
try_files $uri /index.html;
}
# API proxy
Regular → Executable
+2 -1
View File
@@ -23,8 +23,9 @@ server {
client_max_body_size 800m;
# SPA routing - all routes to index.html
# 注意:不能加 $uri/,否则 /assets 等与构建产物目录同名的路由会被当成目录访问,返回 403
location / {
try_files $uri $uri/ /index.html;
try_files $uri /index.html;
}
# API proxy