在 Vue 项目中,能够选择 hash或者 history.pushState() 实现路由跳转。若是在路由中使用history模式:html
export default new Router({ mode: 'history', base: __dirname, scrollBehavior, routes: [index, blog, project, about, list] })
那么,当咱们 npm run build 完成并部署到服务器后,刷新某个路由下的页面,会出现 404 或者 502 错误。vue
这是由于刷新页面时访问的资源在服务端找不到,由于vue-router设置的路径不是真实存在的路径。nginx
解决方法vue-router
简单配置下 nginx ,让全部路由(url)下的页面重写到 index.html便可:npm
server { listen 80; server_name www.fayinme.cn; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_vary off; gzip_disabled "MSIE [1-6]"; autoindex on; root /www/blogfront/production/current; index index.html; location / { try_files $uri $uri/ @router; index index.html; } location @router { rewrite ^.*$ /index.html last; }
注意服务器
配置完成后,若是刷新任意页面(F5)都跳转到首页,你须要查看下 app.vue 是否包含了以下代码:app
created() { this.$router.push('/') }
若是有,注释或删除便可。ui