Nginx高级配置-压缩功能

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.Nginx压缩相关参数概述

1>.gzip on | off;

  Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。

  开源使用"gzip on;"参数来启用压缩,默认是关闭的。

2>.gzip_comp_level lenvel;

  压缩比例由低到高从1到9,默认为1。但需要注意的是压缩比设置的越高就会越消耗CPU的资源,因此在生产环境中我们会设置该参数的值在3~5之间,最好不要超过5,因为随着压缩比的增大的确会降低传输的带宽成本但发送数据前会占用更多的CPU时间分片。

  具体设置级别为多少,得咱们运维人员对CPU的利用率做一个监控,如果CPU利用率过低则不会使用,可以酌情将压缩级别参数调大,当然调大后依旧需要观察一段业务高峰期时间CPU的利用率,最终会找到一个适合你们公司业务的压缩比例。

3>.gzip_disable "MSIE [1-6]\.";

  禁用IE6 gzip功能。

4>.gzip_min_length 1k;

  gzip压缩的最小文件,小于设置值的文件将不会压缩

5>. gzip_http_version 1.0|1.1;

  启用压缩功能时,协议的最小版本,默认HTTP/1.1

6>.gzip_buffers number size;

  指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k;

7>.gzip_types mine-type ...;

  指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错。

8>.gzip_vary on| off;

  如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,建议开启该参数,让用户知道咱们服务端是支持压缩的。

9>.Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_module

博主推荐阅读:
https://nginx.org/en/docs/http/ngx_http_gzip_module.html

二.未启用压缩时观察传输时文件大小

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes ;
worker_cpu_affinity ; events {
worker_connections ;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types; default_type text/html; charset utf-; log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_ti
me,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';
access_log logs/access_json.log my_access_json; include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
} location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
} location /main {
index index.html;
default_type text/html;
set $name jason;
set $nginx_name $server_name;
echo "姓名: $name";
echo "************";
echo "Nginx服务器名称: $nginx_name";
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11890 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11891 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11892 9297 0 12:57 ? 00:00:00 nginx: worker process
nginx 11893 9297 0 12:57 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11946 9297 1 13:25 ? 00:00:00 nginx: worker process
nginx 11947 9297 1 13:25 ? 00:00:00 nginx: worker process
nginx 11948 9297 0 13:25 ? 00:00:00 nginx: worker process
nginx 11949 9297 1 13:25 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#

4>.生成测试数据

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 8
-rw-r--r-- 1 root root 46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root 73 Dec 17 12:54 index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/log/messages
-rw-------. 1 root root 787K Dec 20 06:31 /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# tail -10 /var/log/messages > /yinzhengjie/data/web/nginx/static/test.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cp /var/log/messages /yinzhengjie/data/web/nginx/static/messages.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 root root 46 Dec 17 12:53 default.html
-rw-r--r-- 1 root root 73 Dec 17 12:54 index.html
-rw------- 1 root root 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 root root 746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# chown -R nginx:nginx /yinzhengjie/data/web/nginx/static/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# chmod 644 /yinzhengjie/data/web/nginx/static/messages.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 nginx nginx 46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx 73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 nginx nginx 746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

5>.浏览器访问(小于1k的文件)"http://node101.yinzhengjie.org.cn/test.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

6>.浏览器访问(大于1k的文件)"http://node101.yinzhengjie.org.cn/messages.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

三.启用压缩功能后观察传输文件大小

1>.编辑主配置文件

[root@node101.yinzhengjie.org.cn ~]# cat  -n /yinzhengjie/softwares/nginx/conf/nginx.conf
1 worker_processes 4;
2 worker_cpu_affinity 00000001 00000010 00000100 00001000;
3
4 events {
5 worker_connections 100000;
6 use epoll;
7 accept_mutex on;
8 multi_accept on;
9 }
10
11 http {
12 include mime.types;
13
14 default_type text/html;
15
16 charset utf-8;
17
18 log_format my_default_format '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent"' '"$http_x_fo
rwarded_ 19 for"' '$server_name:$server_port';
20 access_log logs/access.log my_default_format;
21
22 #配置压缩功能相关参数
23 gzip on;
24 gzip_comp_level 5;
25 gzip_min_length 1k;
26 gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application/x-httpd-php image/jpeg image/gif image
/png; 27 gzip_vary on;
28
29 include /yinzhengjie/softwares/nginx/conf.d/*.conf;
30 }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

3>.重新加载nginx配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 11946 9297 0 Dec18 ? 00:00:11 nginx: worker process
nginx 11947 9297 0 Dec18 ? 00:00:00 nginx: worker process
nginx 11948 9297 0 Dec18 ? 00:00:10 nginx: worker process
nginx 11949 9297 0 Dec18 ? 00:00:11 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 Dec17 ? 00:00:00 nginx: master process nginx
nginx 13241 9297 0 06:56 ? 00:00:00 nginx: worker process
nginx 13242 9297 0 06:56 ? 00:00:00 nginx: worker process
nginx 13243 9297 1 06:56 ? 00:00:00 nginx: worker process
nginx 13244 9297 1 06:56 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>.浏览器访问(小于1k的文件)"http://node101.yinzhengjie.org.cn/test.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

5>.浏览器访问(大于1k的文件)"http://node101.yinzhengjie.org.cn/messages.html"并观察响应报文的“Content-Length”属性值,发现文件时没有被压缩的,如下图所示。

  点击"message.html"文件的响应信息,如下图所示。

  第二次访问"http://node101.yinzhengjie.org.cn/messages.html"时,可以看不到大小了,如下图所示。

关闭所有浏览器标签实例,重新打开浏览器访问,如下图所示。

6>.开启压缩功能后,只要打开浏览器访问web服务器,发现物理机的CPU就开始飙升了(因为我的虚拟机是在笔记本中搭建的),如下图所示

  使用任务管理器的进程按照CPU使用率查看,的确是Google占用带宽了,我估计是谷歌浏览器在对压缩的数据进行解压操作,压缩和解压过程均需要占用CPU时间片。

当关闭了所有浏览器标签后,一切回归了正常。CPU使用率始终位置在10%以下了

四.当文件被修改时Etag会发生变化

1>.服务端修改源文件内容

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 800
-rw-r--r-- 1 nginx nginx 46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx 73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 805343 Dec 20 06:40 messages.html
-rw-r--r-- 1 nginx nginx 746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/messages.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Fri, 20 Dec 2019 00:23:55 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 805343
Last-Modified: Thu, 19 Dec 2019 22:40:43 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5dfbfc6b-c49df"
Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# tail -500 /var/log/messages > /yinzhengjie/data/web/nginx/static/messages.ht
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/messages.html
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Fri, 20 Dec 2019 00:24:43 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 43543
Last-Modified: Fri, 20 Dec 2019 00:24:40 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "5dfc14c8-aa17"
Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/static/
total 56
-rw-r--r-- 1 nginx nginx 46 Dec 17 12:53 default.html
-rw-r--r-- 1 nginx nginx 73 Dec 17 12:54 index.html
-rw-r--r-- 1 nginx nginx 43543 Dec 20 08:24 messages.html
-rw-r--r-- 1 nginx nginx 746 Dec 20 06:40 test.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.浏览器再次访问时并不会使用默认的本地缓存,这是为什么呢?(这需要咱们去看看该文件的响应报文头部)

3>.我们发现当ETag的值发生改变时,并不会使用本地缓存啦,而是会发送新的请求,获取文件的最新状态。

Nginx 高级配置-压缩功能的更多相关文章

  1. Nginx 高级配置-https 功能

    Nginx 高级配置-https 功能 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTTPS工作过程 1>.SSL/TLS SSL(Secure Socket Lay ...

  2. Nginx 高级配置-实现多域名HTTPS

    Nginx 高级配置-实现多域名HTTPS 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx支持基于单个IP实现多域名的功能 Nginx支持基于单个IP实现多域名的功能 ...

  3. Nginx 高级配置-变量使用

    Nginx 高级配置-变量使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用,变量可以分为内置变量和自定义变 ...

  4. Nginx 高级配置-第三方模块编译

    Nginx 高级配置-第三方模块编译 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数--add ...

  5. Nginx 高级配置-状态页配置

    Nginx 高级配置-状态页配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 建议将nginx的监控状态的值通过zabbix或者Open-Falcon之类的监控工具来监控状态,并 ...

  6. Nginx开启gzip压缩功能

    在Nginx安装完成之后,我们可以开启Gzip压缩功能,这里Nginx默认只能对text/html类型的文件进行压缩.下面的指令为开启Gzip的指令: gzip on; gzip_http_versi ...

  7. Nginx 高级配置--关于favicon.ico

    Nginx 高级配置--关于favicon.ico 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浏览器会默认帮咱们访问官网的图标 1>.浏览器访问网站"htt ...

  8. Nginx 高级配置-自定义json格式日志

    Nginx 高级配置-自定义json格式日志 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在大数据运维工作中,我们经常会使用flume,filebeat相关日志收集工具取收集日志 ...

  9. Nginx 之四: Nginx服务器的压缩功能和缓存功能

    在Nginx服务器配置文件中可以通过配置Gzip的使用,可以配置在http块,server 块或者location块中设置,Nginx服务器可以通过ngx_http_gzip_module模块.ngx ...

随机推荐

  1. Python进阶-V 迭代器(Iterator)、生成器(Generator)函数

    一.迭代器 1.可循环的有哪些,即可用for语句或者while语句的数据类型有哪些? 字符串(str).列表(list).元组(tuple).字典(dic).集合(set).枚举类(enumerate ...

  2. [ Python入门教程 ] Python的控制语句

    Python控制语句由条件语句.循环语句构成.控制语句根据条件表达式控制程序的流转.本章将介绍Python中控制语句的基本语法. 条件判断语句 (1)if条件语句 if语句用于检测某个条件是否成立.如 ...

  3. Python程序设计例题

    例一:蒙特卡罗方法求解 π 值 from random import random from math import sqrt from time import clock DARTS=1000 hi ...

  4. Jenkins的简介及安装

    Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 1. jenkins就是 ...

  5. Asp.Net Core 减少Controller获取重复注入对象

    原文:Asp.Net Core 减少Controller获取重复注入对象 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012770274/art ...

  6. java ++前缀

    public class Sample { public static void main(String[] args) { , num2 = ; , num4 = ; ++num1; System. ...

  7. Leetcode 第137场周赛解题报告

    今天的比赛的题目相对来说比较「直白」,不像前几周都是一些特定的算法,如果你没学过不可能想出来. 做了这些周,对leetcode比赛的题目也发现了一些「规律」. 一般前两道题都很「简单」,只要有想法,直 ...

  8. 推荐一款移动端天气App即刻天气

    推荐一款移动端天气App即刻天气 一 应用描述 即刻天气预报是一个提供全国各城市15日天气预报和空气质量的APP,包含全国3000个城市天气预报,3万个乡镇天气,15日及48小时空气质量预报,是万千用 ...

  9. 多线程避免使用SimpleDateFormat及替代方案

    先来看一个多线程下使用例子,看到运行结果会出现异常: import java.text.DateFormat; import java.text.SimpleDateFormat; import ja ...

  10. W5500电路图

    W5500是韩国一款集成全硬件 TCP/IP 协议栈的嵌入式以太网控制器,W5500同时也是一颗工业级以太网控制芯片,最近发现我们国内也有和W5500 芯片一样芯片 介绍给大家 如下图: