vue路由history模式刷新页面出现404问题

9次阅读

共计 620 个字符,预计需要花费 2 分钟才能阅读完成。

vue hash 模式下,URL 中存在 ’#’,用 ’history’ 模式就能解决这个问题。但是 history 模式会出现刷新页面后,页面出现 404。解决的办法是用 nginx 配置一下。在 nginx 的配置文件中修改
方法一:
location /{
root /data/nginx/html;
index index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
break;
}
}
方法二:vue.js 官方教程里提到的 https://router.vuejs.org/zh/g…
server {
listen 8081;# 默认端口是 80,如果端口没被占用可以不用修改
server_name myapp.com;
root D:/vue/my_app/dist;#vue 项目的打包后的 dist
location / {
try_files $uri $uri/ @router;# 需要指向下面的 @router 否则会出现 vue 的路由在 nginx 中刷新出现 404
index index.html index.htm;
}
#对应上面的 @router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要 rewrite 到 index.html 中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
#……. 其他部分省略
}

正文完
 0