【发布时间】:2019-02-27 11:22:24
【问题描述】:
我已经设置了我的 vue-cli 版本 3 SPA,以便在我的 routes.js 文件中找不到的任何请求都将默认为我的 404 视图,如官方 documentation 所示:
插入到routes.js 文件底部附近:
{ // catches 404 errors
path: '*',
name: '404',
component: () => import(/* webpackChunkName: "NotFoundComponent" */ './views/NotFoundComponent.vue'),
},
插入到nginx配置文件中:
location / {
try_files $uri $uri/ /index.html;
}
这成功地提醒用户他们请求的页面不存在。
我的问题:
我希望错误 404 组件返回 404 响应标头(它当前返回 200 状态代码)并将此错误记录到 nginx error.log 文件中。我想这只能通过使用 nginx 配置来实现。有人实现了这个目标吗?
我注意到这个问题在 vue-cli 官方文档的以下页面中得到了解决,但它只与 node express 服务器有关,而不是 nginx: https://router.vuejs.org/guide/essentials/history-mode.html#caveat
【问题讨论】:
-
试试这个
try_files $uri $uri/ /index.html =404; -
在这篇文章中查看我的答案:stackoverflow.com/a/62651493/2951619
标签: nginx vue.js vue-router vue-cli-3