vue2和nginx配置Gzip

减小js和css文件大小,提高首屏速度。
虽然也没有快到哪里去(可能是因为我没有调参),但js、css文件的大小都大幅度减小了。
参考:https://blog.csdn.net/qq_43363884/article/details/108195408

nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    # 这里是使用了优先静态gzip(因为在vue打包的时候已经gzip了,优先查找压缩过的js或css,没有的话则在服务器上gzip)
    gzip on;
    gzip_static on;
    gzip_comp_level 2;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            #因为路由是history模式,所以要try_files
            try_files $uri $uri/ /index.html;
        }

        location /api/ {
          proxy_pass http://127.0.0.1:8080/;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

vue.config.js

const path = require('path')

const webpack = require('webpack')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']

module.exports = {
  transpileDependencies: ["vuetify"],
  devServer: {
    proxy: "http://101.200.171.192/",
  },
  assetsDir: "static",
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@i': path.resolve(__dirname, './src/assets'),
      },
    },
    plugins: [
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      new CompressionWebpackPlugin({
        algorithm: 'gzip',
        test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
        threshold: 10240,
        minRatio: 0.8
      }),
      new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 5,
        minChunkSize: 100
      })
    ]
  }
};

Original: https://www.cnblogs.com/m1pha/p/16258060.html
Author: m1pha
Title: vue2和nginx配置Gzip

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582624/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • shell 获取变量是什么数据类型

    bash;gutter:true; function check(){ local a="$1" printf "%d" "$a&…

    Linux 2023年5月28日
    072
  • Linux 0.11源码阅读笔记-文件IO流程

    文件IO流程 用户进程read、write在高速缓冲块上读写数据,高速缓冲块和块设备交换数据。 何时将磁盘块数据读取到缓冲块? [En] when will the disk bl…

    Linux 2023年5月27日
    071
  • Redis16个常见使用场景

    目录 缓存 数据共享分布式 分布式锁 全局ID 计数器 限流 位统计 购物车 用户消息时间线timeline 消息队列 抽奖 点赞、签到、打卡 商品标签 商品筛选 用户关注、推荐模…

    Linux 2023年5月28日
    0103
  • 在c/c++中输入彩色日志输出,带有带有颜色的打印

    在c/c++中输入彩色日志输出,带有带有颜色的打印 #ifndef __PTINTCOLOR_H #define __PTINTCOLOR_H #include #ifndef L…

    Linux 2023年6月14日
    0125
  • Bitmap在Redis中的应用(转)

    原文:https://cloud.tencent.com/developer/news/387248 作者:一叶而不知秋 作为铺垫,我们先来介绍一些Bitmap的相关内容: 位图主…

    Linux 2023年5月28日
    080
  • Python中使用%s占位符生成sql与literal转义防止sql注入攻击原理浅析

    问题背景 在后端服务中经常需要通过传入参数动态生成sql查询mysql,如查询用户信息、资产信息等,一条常见的sql如下: SELECT vip, coin FROM user_a…

    Linux 2023年6月6日
    0122
  • Jenkins+svn自动化部署完整教程

    1、概述 Jenkins 是一个可扩展的持续集成引擎。主要用于持续、自动地构建/测试软件项目、监控一些定时执行的任务。Jenkins用Java语言编写,可在Tomcat等流行的se…

    Linux 2023年6月7日
    084
  • 【MQTT】基于mosquitto的学习和使用

    文章目录 前言 一、MQTT简介 * MQTT特性 MQTT协议原理 MQTT协议数据包结构 二、安装MQTT * 1.mosquitto简介 2.安装mosquitto库 2.常…

    Linux 2023年6月13日
    0114
  • MySQL表空间回收的正确姿势

    不知道大家有没有遇到这样的一种情况,线上业务在MySQL表上做增删改查操作,随着时间的推移,表里面的数据越来越多,表数据文件越来越大,数据库占用的空间自然也逐渐增长 为了缩小磁盘上…

    Linux 2023年6月13日
    082
  • docker容器编排原来这么丝滑~

    前言: 请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 概念介绍: Docker 这个东西所扮演的角色,容易理解,它是一个容器引擎,也就是说实际上我们的容器最终是由Doc…

    Linux 2023年6月14日
    086
  • 我最鄙视的程序员

    今天在技术群里看到关于优秀程序员特质的话题,让我想起多年前的一个同事,一个我最鄙视的程序员。 他的名字叫李伟(化名),是入职没多久的员工,我所在的开发二组和他在的一组,以前没有过工…

    Linux 2023年6月6日
    080
  • Spring Boot连接数据库,从MySql5.13飞跃到MySql8.0.18的坑

    Spring Boot连接数据库,从MySql5.13飞跃到MySql8.0.18的坑 1.驱动名 驱动包用的是:mysql-connector-java-8.0.18.jar 驱…

    Linux 2023年6月7日
    076
  • Redis

    一、Redis的介绍 什么是Redis Redis是一个开源的使用ANSI C语言编写Key-Value内存数据库 读写性能强,支持多种数据类型 把数据存储在内存中的高速缓存 Re…

    Linux 2023年6月14日
    071
  • CentOS-7配置fastDFS文件服务器和安装Nginx

    配置步骤实在是很繁琐,听我慢慢道来! 主要是配置管理(tracker)和存储(storage)返回地址样式 –> 域名/组名/磁盘名/目录名/文件名 &#8211…

    Linux 2023年5月27日
    075
  • Linux 下安装 node.js

    这里介绍两种安装方式: 编译安装和使用编译后的安装包安装。 安装目录: /usr/local 一、使用编译安装包安装 1、进入安装目录: 2、下载安装包: 3、解压: 4、进入解压…

    Linux 2023年6月13日
    088
  • c++ 使用shell命令

    #include #include #include #include #include //execute shell command //执行&#x…

    Linux 2023年5月28日
    091
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球