[英]how to use ngnix to serve webpack build files in production
I wrote a vue + webpack project and it works fine in webpack-dev-middleware.我写了一个 vue + webpack 项目,它在 webpack-dev-middleware 中运行良好。 Now I want to deploy it with nginx.
现在我想用 nginx 部署它。 What I do is write a webpack.build.config.js and bundle all files into a dist folder.
我所做的是编写 webpack.build.config.js 并将所有文件捆绑到 dist 文件夹中。 Then I just copy the dist folder into nginx html folder and assign the index in nginx.conf.
然后我只需将 dist 文件夹复制到 nginx html 文件夹中,并在 nginx.conf 中分配索引。 However, it has an error said:
但是,它有一个错误说:
[Vue warn]: Failed to mount component: template or render function not defined.
[Vue 警告]:无法安装组件:模板或渲染 function 未定义。 (found in root instance)
(在根实例中找到)
I am a newbie for devops/backend and quite confused with the overall build or deploy process.我是 devops/后端的新手,对整个构建或部署过程很困惑。 Is webpack-dev-server or nodejs still need in the production environment?
生产环境还需要 webpack-dev-server 或 nodejs 吗? My production environment backend is nginx/PHP and IIS/.Net, now it do not have node installed at all.
我的生产环境后端是 nginx/PHP 和 IIS/.Net,现在它根本没有安装节点。
My nginx.conf is我的 nginx.conf 是
location / {
root html/dist;
index index.html index.htm;
}
And the webpack.build.config.js is webpack.build.config.js 是
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var public_dir = "components";
//var ModernizrWebpackPlugin = require('modernizr-webpack-plugin');
module.exports = {
entry: [
path.join(__dirname,'./index.js')
],
output: {
path: path.join(__dirname, '/dist/'),
filename: '[name].js',
publicPath: '/'
},
devtool: 'eval-source-map',
plugins: [
new webpack.optimize.CommonsChunkPlugin('common.js'),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.resolve(__dirname, 'index.html'),
inject: true
})
],
resolve: {
root: [path.resolve('./components')],
extensions: ['', '.js', '.css']
},
module: {
loaders: [
]
}
};
When build I run构建时我运行
webpack -p --config./webpack.build.config.js
webpack -p --config./webpack.build.config.js
I'm using vue-cli to init vuejs webpack project.我正在使用vue-cli来初始化 vuejs webpack 项目。 And the project already has build script, you can refer it:
并且项目已经有构建脚本,可以参考:
require('./check-versions')()
process.env.NODE_ENV = 'production'
var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')
var spinner = ora('building for production...')
spinner.start()
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err
webpack(webpackConfig, function (err, stats) {
spinner.stop()
if (err) throw err
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
})
After built, we will have a dist
folder.构建后,我们将有一个
dist
文件夹。 Upload all files inside to html
folder of Nginx (default) Config root path to use full path like this:将里面的所有文件上传到
html
的 html 文件夹(默认) 配置根路径以使用完整路径,如下所示:
listen 80;
server_name mydomain www.mydomain;
root /var/www/html;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.