对nginx配置文件.conf修改
server {
listen 80;
server_name www.xxx.com;
location / {
index /data/dist/index.html;
try_files $uri $uri/ /index.html;
}
}
重启
nginx -s reload
前端路由添加404页面
这么做以后,你的服务器就不再返回 404 错误页面,因为对于所有路径都会返回 index.html 文件
为了避免这种情况,你应该在 Vue 应用里面覆盖所有的路由情况,然后在给出一个 404 页面
const router = new VueRouter({
mode: ‘history’,
routes: [
{ path: ‘*’, component: NotFoundComponent }
]
})

本文介绍了如何修改Nginx配置文件以处理前端Vue应用的路由,确保所有路径都能返回index.html,避免404错误。通过设置`try_files`指令并添加Vue Router的404捕获路由,可以实现所有未定义路径重定向到404页面,从而提供更好的用户体验。
980

被折叠的 条评论
为什么被折叠?



