vue路由history模式配置

it2024-07-04  41

vue路由history模式刷新页面时出现404问题的两种解决方法

本篇文章给大家带来的内容是关于vue路由history模式刷新页面时出现404问题的两种解决方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 vue hash模式下,URL中存在’#’,用’history’模式就能解决这个问题。但是history模式会出现刷新页面后,页面出现404。解决的办法是用nginx配置一下。

在nginx的配置文件中修改

方法一:
location /{ root /data/htdocs/app; #服务器上项目存放路径 index index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.html last; break; } }
方法二:

vue.js官方教程里提到的 点击查看

server { listen 8081; #默认端口是80,如果端口没被占用可以不用修改 server_name myapp.com; root /data/htdocs/app; #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; } #.......其他部分省略 }

以上就是vue路由history模式刷新页面时出现404问题的两种解决方法的详细内容,经测试都可行。

附:有关Nginx配置前端项目,请查看这篇文章:Linux服务器部署前端项目到外网访问

最新回复(0)