前言:vue cli3的性能优化里面,开启gzip能获得不少的收益。经过webpack插件compression-webpack-plugin能够在打包的时候生成.gz文件;当用nginx作服务器时,nginx经过_gzip on;_配置可对每一个请求先压缩再输出,这样形成虚拟机浪费了不少cpu;并且webpack打包时已经生成了压缩文件,彻底不必从新经过nginx再压缩一下。发现这个问题后,经过半天的资料查询找到了答案:**nginx gzip static静态压缩,**下面把我解决的过程记录一下。javascript
1、配置vue cli3 gzipcss
const CompressionWebpackPlugin = require('compression-webpack-plugin') module.exports = { configureWebpack: config => { // 开发环境不须要gzip if (process.env.NODE_ENV !== 'production') return config.plugins.push( new CompressionWebpackPlugin({ // 正在匹配须要压缩的文件后缀 test: /\.(js|css|svg|woff|ttf|json|html)$/, // 大于10kb的会压缩 threshold: 10240 // 其他配置查看compression-webpack-plugin }) ) } }
2、安装nginx ngx_http_gzip_module模块html
先下载nginxvue
cd /nginx解压目录java
./configure --prefix=/usr/local/nginx --with-http_gzip_static_modulewebpack
上面的/usr/local/nginx为nginx安装目录,可根据本身喜爱修改nginx
makegit
make installgithub
3、配置nginxweb
找到/usr/local/nginx/conf/nginx.conf,并添加下面代码
server { listen 4300; server_name localhost; location / { root /home/static/web/wechat; index /index.html; try_files $uri $uri/ /index.html; gzip_static on; #静态压缩 } }
启动nginx服务:./nginx -c /usr/local/nginx/conf/nginx.conf
4、查看效果
1.打包后文件
2. 浏览器访问资源结果