vue的history模式nginx配置

当上下文不是根目录的时候,比如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被浏览器加载之后,前端路由就会工作,将用户需要的资源加载出来。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容