Vue项目使用Nginx部署并发布到子目录
Vue项目使用Nginx部署并发布到子目录
默认Vue
项目是以根目录来发布的,但是我们要有时候希望以子目录来做发布,那么就需要做一些修改,Vue
项目本身的配置,包括Nginx
也需要做一些配置修改。
对Vue项目源码的修改
以下路径以
vue-cli
常见项目的文件结构来说明。
-
修改路由:
router/index.js
,添加base
部分的代码。1 2 3 4 5 6 7 8 9 10
function getAbsolutePath () { let path = location.pathname return path.substring(0, path.lastIndexOf('/') + 1) } export default new Router({ mode: 'hash', base: getAbsolutePath(), routes: [...] })
-
修改配置:
config/index.js
文件,对于打包路径的定义,只处理build
,将assetsPublicPath: '/'
修改为assetsPublicPath: './'
1 2 3 4 5 6 7 8 9 10 11 12
module.exports = { build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: './', ... } }
-
修改
build/utils.js
对静态文件的处理,添加publicPath: '../../'
1 2 3 4 5 6 7 8 9 10 11 12
// Extract CSS when that option is specified // (which is the case during production build) if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader', publicPath: '../../' }) } else { return ['vue-style-loader'].concat(loaders) } }
对Nginx配置的修改
当前项目结构如下:
game
├── bxfr
│ ├── index.html
│ └── static
│ ├── css
│ ├── images
│ ├── img
│ ├── js
│ └── mock
└── fwjt
├── index.html
└── static
├── css
├── images
├── img
├── js
└── mock
而我们要实现的目的是:使用my.server.name/bxfr/
和my.server.name/fwjt/
分别访问不同的项目。
|
|
- 原文作者:CsHeng
- 原文链接:https://sukikaka.cc/2019/07/11/vue%E9%A1%B9%E7%9B%AE%E4%BD%BF%E7%94%A8nginx%E9%83%A8%E7%BD%B2%E5%B9%B6%E5%8F%91%E5%B8%83%E5%88%B0%E5%AD%90%E7%9B%AE%E5%BD%95/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。