美文网首页
vue的history模式nginx配置

vue的history模式nginx配置

作者: 依风听雨锋 | 来源:发表于2022-08-17 14:04 被阅读0次

    当上下文不是根目录的时候,比如app。如下配置

    路由配置

    //路由对象
    const router = createRouter({
        // history: createWebHashHistory(),
        history:createWebHistory('/app'),
        routes //上面的路由数组
    
    })
    

    配置打包时候,上下文
    vite.config.js

    export default defineConfig({
        base: 'app',
        ....
    })
    

    nginx配置

                  location /app {
                            alias /home/zou/www/app;
                            index index.html index.htm;
                            try_files $uri $uri/ /app/index.html;
                    }
    

    最重要的是 try_files $uri $uri/ /app/index.html;http://www.example.com/post为例,$uri会匹配到post,nginx发现dist下面没有post这个文件,也没有post这个文件夹,所以最后会返回index.html。这样,index.html被浏览器加载之后,前端路由就会工作,将用户需要的资源加载出来。

    相关文章

      网友评论

          本文标题:vue的history模式nginx配置

          本文链接:https://www.haomeiwen.com/subject/jaeegrtx.html