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

版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 https://blog.csdn.net/qq_43168841/article/details/83019082

vue hash模式下,URL中存在’#’,用’history’模式就能解决这个问题。但是history模式会出现刷新页面后,页面出现404。解决的办法是用nginx配置一下。
在nginx的配置文件中修改
vue路由history模式刷新页面出现404问题
方法一:

location /{
 root /data/nginx/html;
 index index.html index.htm;
 if (!-e $request_filename) {
 rewrite ^/(.*) /index.html last;
 break;
 }
}

方法二:

 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;
 }
 #.......其他部分省略
 }

本次给大家推荐一个免费的学习群,里面概括移动应用网站开发,css,html,webpack,vue node angular以及面试资源等。
对web开发技术感兴趣的同学,欢迎加入Q群:864305860,不管你是小白还是大牛我都欢迎,还有大牛整理的一套高效率学习路线和教程与您免费分享,同时每天更新视频资料。
最后,祝大家早日学有所成,拿到满意offer,快速升职加薪,走上人生巅峰。

猜你喜欢

转载自blog.csdn.net/qq_43168841/article/details/83019082