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)

大家都在看

  • C语言实现扫雷游戏(完整版)

    头文件定义、函数声明 下面就是扫雷中使用到的所有函数,为了省事我把所有的代码都放在一个C文件中实现 宏定义中设置了游戏的界面布局,以及设置地雷的个数(这里默认的是10个地雷),界面…

    Linux 2023年6月6日
    0137
  • Spring Boot 项目启动错误 提示 java.lang.ClassNotFoundException org.apache.log4j.Logger

    问题描述 spring boot项目升级到2.x,启动时出现错误提示:java.lang.ClassNotFoundException: org.apache.log4j.Logg…

    Linux 2023年6月14日
    095
  • k8s多集群切换:使用kubeconfig文件管理多套kubernetes(k8s)集群

    一.系统环境 二.前言 三.kubeconfig文件 四.kubernetes(k8s)多集群切换 一.系统环境 服务器版本 docker软件版本 CPU架构 CentOS Lin…

    Linux 2023年6月7日
    0135
  • WPF 调试依赖属性变更方法

    本文告诉大家如何调试 WPF 的某个依赖属性被变更的方法 在 WPF 里面,所有的依赖属性都有带通知的功能,通过带通知的功能,可以在通知里加上断点,通过调用堆栈了解是哪个模块调用的…

    Linux 2023年6月6日
    093
  • 四年测试的面试题分享

    其实想说为什么每次面试都要先来点自我介绍,说来说去简历上都有,我曾想过不能快速进入面试阶段嘛 我的专业技能: 基本上这些专业技能是在工作上用过或者自己摸索过,实战经验比较少,下面是…

    Linux 2023年6月8日
    084
  • 数据库的灾备

    数据是企业重要的生产资料,关键数据的丢失可能会给企业致命一击,因为数据是计算机系统存在的原因和基础。数据往往是不可再生的,一旦发生数据丢失,企业就会陷入困境:客户资料、技术文件、财…

    Linux 2023年6月6日
    0108
  • docker相关命令杂理

    – 2020.11.16docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] #保存现有的镜像 # docker commit …

    Linux 2023年6月8日
    091
  • 同城双活概述

    引言 同城双活,是年度最大的架构变更。同城容灾,对于生产的高可用保障,重大的意义和价值是不言而喻的。 用储总的话说,这么重要的架构工作,所有架构师都应该重点主导和参与。 同城双活,…

    Linux 2023年6月14日
    0125
  • JavaScript快速入门-05-基本语句

    5 基本语句 5.1 if 语句 if 语句常用语法如下所示: if (condition) { statement1; } else { statement2; } 或 if (…

    Linux 2023年6月7日
    0144
  • c++ 使用shell命令

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

    Linux 2023年5月28日
    0105
  • django Middleware

    Middleware简介 Middleware是一个轻量级的,全局性质的Django请求/响应处理钩子框架。所谓钩子框架是指在request请求到达Django之后,views视图…

    Linux 2023年6月7日
    0104
  • 搭建Nginx四层反向代理

    需求背景: 前段时间公司因为业务需求需要部署一个正向代理,我已经分享出来了https://www.cnblogs.com/Dfengshuo/p/11911406.html,现有因…

    Linux 2023年6月8日
    0116
  • 在Linux下安装Redis

    1. 下载Redis的压缩包 [root@spirit-of-fire ~]# wget http://download.redis.io/releases/redis-5.0.5…

    Linux 2023年6月14日
    097
  • vue-admin-template组件前端,登录验证成功后,getInfo方法不调用

    先查看request.js中自定义状态码是否为自己的定义的成功状态码 这里的状态码该为自己的成功状态码 Original: https://www.cnblogs.com/antl…

    Linux 2023年6月7日
    0107
  • Redis集群-主从架构

    1.为什么需要集群? 互联网思维一向讲究的是三高,即高并发,高可用,高性能。高并发:系统能够同时并行处理的请求,主要度量指标有响应延时,吞吐量,每秒查询处理,每秒事务处理,并发用户…

    Linux 2023年6月7日
    0118
  • 【Leetcode】64. 最小路径和

    给定一个包含非负整数的 m x n网格 grid,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动…

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