部署前端代码时,基于 Nginx 配置,一旦刷新则会响应 404 的解决

1、在进入前端页面后,点击一些链接,网址会发生变化,此时响应皆是 200。但是一旦刷新整个页面,则会响应 404。如视频1

2、查看 Nginx 的错误日志,添加一行:try_files $uri $uri/ /index.html;

2024/01/04 10:19:17 [error] 23408#17516: *244 CreateFile() "E:/wwwroot/object/dist/apps/admin/order/order-center/orders" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: object.local, request: "GET /order/order-center/orders HTTP/1.1", host: "object.local"
    index index.html;

 try_files $uri $uri/ /index.html;

3、再次刷新整个页面,不再响应 404。

永夜