一、vue工程配置
index.html 添加<meta base="/XXX/"> XXX为nginx html下的项目文件夹
router文件夹下 index.js
export default new Router({
base: '/XXX/',
// mode: 'history', //后端支持可开
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})
config文件夹下 index.js 添加assetsPublicPath: '/XXX/'
二、nginx配置
location / {
root html;
index index.html index.htm;
}
location /XXX{
root html;
try_files $uri $uri/ /XXX/index.html;
}
location /YYY{
root html;
try_files $uri $uri/ /YYY/index.html;
}
其中 root html 可以拎出来放在外面,作为公用的,如果项目都在同一个文件夹下的话。
三、碰到的问题
先前在网上找了好多资料,访问页面一直是500,苦于找不到方法,后面看了一下 location和root的意思,才发现是自己理解错了。
先前的代码是
location /demo{
root html/demo;
try_files $uri $uri/ /demo/index.html;
index index.html index.htm;
}
以为nginx是从 root下找 index对应的页面,我以为是 html/demo/index.html,觉得没有问题。后来才发现nginx是依据 root 和 try_files 找页面,我这样写,其实访问的是 html/demo/demo/index.html。难怪nginx一直访问不到。所以root应该改成html,这样访问的就是html/demo/index.html。
写下来给自己一个教训。