nginx下配置支持vue程序的historyModel

2021-10-31 509点热度 0人点赞 0条评论

默认的vue程序路由使用的是#hash模式,地址栏上会出现http://localhost:8080/#/user 这样的,比较难看,对于强迫症晚期患者来说,必须要解决。

我们在开发中,很容易解决,只需要在router/index.js下找到如下代码修改成:

const router = new VueRouter({
    mode: 'history', # 这里加上,就换成history模式了
    base: process.env.BASE_URL,
    routes
})

export default router

开发过程中没问题了,浏览器地址栏已经变成了http://localhost:8080/user了,不过部署到服务器上后(nginx),如果用户直接在地址栏输入http://localhost:8080/user,就会出现404错误了!

其实这种情况,nginx只需要配置文件修改一下配置就可以了(只需要配置一个location try_files默认指向index.html即可),来看下:

server {
    listen       80;
    server_name  _;
    root         /var/www/website;

    location / {
        # 支持vue的history模式,配置这里2行
        add_header Cache-Control 'no-store, no-cache';
        try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论