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)

大家都在看

  • Redis基础

    1.简介 Redis (远程字典服务器)是一 个开源的、使用C语言编写的NoSQL数据库。Redis基于内存运行并支持持久化,采用 key-value (键值对)的存储形式,是目前…

    Linux 2023年6月13日
    080
  • Debian10 命令行启动

    打开 default grub 配置 $ sudo vi /etc/default/grub 修改以下3处内容 保存修改 更新grub配置 $ sudo update-grub 设…

    Linux 2023年5月27日
    0100
  • redis批量删除key 远程批量删除key

    一、遇到的问题 在开发的过程中,经常会遇到要批量删除某种规则的key,如缓存的课程数据”course-课程uid”,其中课程uid是变量,我们需要删除&#8…

    Linux 2023年5月28日
    080
  • Question07-查询学过”张三”老师授课的同学的信息

    * SELECT DISTINCT Student.* FROM Student , SC , Course , Teacher WHERE Student.SID = SC.SI…

    Linux 2023年6月7日
    0108
  • Linux嵌套目录权限的比较探究

    在/tmp目录下新建一个嵌套目录,名字分别为test_0、test_1、test_2。在test_2目录下新建普通文件,名为tryme。设置test_0和test_2的权限为777…

    Linux 2023年6月7日
    086
  • 关于Java访问SQL server的错误:The server selected protocol version TLS10 is not accepted by client preferences [TLS12]及安全套接字层(SSL)加密与 SQL Server 建立安全连接

    此笔记记载了本人在使用centos7.6环境下使用java连接sqlserver2008时 The server selected protocol version TLS10 i…

    Linux 2023年6月14日
    092
  • 误删除系列二:恢复已经删除文件

    背景:基于对恢复的好奇心,所以写一系列相关的博客,在linux没有回收站这一说法,通过rm -rf file的操作,如何恢复 以下的讨论分为两种情况: 删除后进程还能找到情况 删除…

    Linux 2023年6月7日
    070
  • CentOS 7.6 安装 MySQL-5.7.31(RPM方式安装)

    准备工作: 注:5.7.31版本安装步骤及初始化和之前版本有较大区别 CentOS 7.6 系统: 带GUI的服务器 默认安装 MySQL 5.7.31 安装包: 1.RPM安装包…

    Linux 2023年6月8日
    086
  • Java基础之接口篇

    Overload和Override的区别?重载Overload:表示同⼀个类中可以有多个名称相同的⽅法,但这些⽅法的参数列表各不相同,参 数个数或类型不同 重写Override:表…

    Linux 2023年6月7日
    095
  • redis 命令

    Redis是用C语言实现的,一般来说C语言实现的程序”距离”操作系统更近,执行速度相对会更快。 Redis使用了单线程架构,预防了多线程可能产生的竞争问题。…

    Linux 2023年5月28日
    079
  • Snap Build Your Own Block修炼之道-添加自定义类别

    Snap Build Your Own Block自我修炼方法:1、所有的面向对象,其实是对面向过程的抽象过程而已; 2、面对别人的开源项目时,需要找准源头(即项目运行的起点,当然…

    Linux 2023年6月6日
    0100
  • 5.4 Linux Vim基本操作

    《Vim三种工作模式》一节给大家详细介绍了 Vim 的 3 种工作模式,本节来学习如何使用 Vim 编辑文件。 首先学习如何使用 Vim 打开文件。 Vim 打开文件 使用 Vim…

    Linux 2023年6月7日
    099
  • 教你写Spring组件

    一、宗旨 在如日中天的 Spring 架构体系下,不管是什么样的组件,不管它采用的接入方式如何眼花缭乱,它们永远只有一个目的: 接入Spring容器 二、Spring 容器 Spr…

    Linux 2023年6月6日
    0120
  • 小白上手Linux系统安装jdk教程

    Eg:将上传后的jdk,解压到/home/lzh/jdk目录下,命令如下: tar -zxvf ./ jdk 版本号 -C /home/lzh/jdk/ 注意末尾必须加&#8221…

    Linux 2023年5月27日
    087
  • centos8 redis安装

    redis的优势我就不多说了(其实我也就是跟着主流) 既然想试试redis,那么就从linux下安装redis开始。那么windows下可以安装redis吗? 答案是肯定的,但是官…

    Linux 2023年5月28日
    0120
  • shell相关知识1

    组命令,就是将多个命令划分为一组,或者看成一个整体。 用法区别 Shell 组命令的写法有两种: { command1; command2;. . .; }(command1; c…

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