美文网首页VueWeb Developer
Vue开启Gzip Nginx开启Gzip

Vue开启Gzip Nginx开启Gzip

作者: ChangLau | 来源:发表于2018-09-30 15:45 被阅读247次

    安装compression-webpack-plugin

    npm compression-webpack-plugin --save-dev
    
    

    vue.config.js配置Gzip压缩

    // 导入compression-webpack-plugin
    const CompressionWebpackPlugin = require('compression-webpack-plugin')
    // 定义压缩文件类型
    const productionGzipExtensions = ['js', 'css']
    
    module.exports = {
      // 配置反向代理
      devServer: {
        proxy: {
          '/api': {
            target: 'http://172.31.120.61:8080/',
            // target: 'http://172.31.120.158:8080/',
            ws: true,
            changeOrigin: true,
            pathRewrite: {
              '^/api': ''
            }
          }
        }
      },
      configureWebpack: {
        plugins: [
          new CompressionWebpackPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
            threshold: 10240,
            minRatio: 0.8
          })
        ]
      }
    }
    

    配置Nginx

     gzip on; #开启或关闭gzip on off
     gzip_disable "msie6"; #不使用gzip IE6
     gzip_min_length 100k; #gzip压缩最小文件大小,超出进行压缩(自行调节)
     gzip_buffers 4 16k; #buffer 不用修改
     gzip_comp_level 8; #压缩级别:1-10,数字越大压缩的越好,时间也越长
     gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #  压缩文件类型 
    

    相关文章

      网友评论

        本文标题:Vue开启Gzip Nginx开启Gzip

        本文链接:https://www.haomeiwen.com/subject/vzpwoftx.html