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. web框架--tornado框架之模板引擎

    使用Tornado实现一个简陋的任务表功能demo来讲解tornado框架模板引擎 一.demo目录结构 二.具体文件内容 2.1.commons.css .body{ margin: 0; back ...

  2. KDE 上 任务栏 图标位置更改

    任务栏上图标是不能直接拖拽的. 右键点任务栏,选[Panel Options -> Panel Settins]之后,就可以拖拽了. 完成之后,按X就行了.

  3. 【Step-By-Step】第 三 周

    本周面试题一览: 什么是XSS攻击,XSS 攻击可以分为哪几类?我们如何防范XSS攻击? 如何隐藏页面中的某个元素? 浏览器事件代理机制的原理是什么? setTimeout 倒计时为什么会出现误差? ...

  4. 洛谷P2194 【HXY烧情侣】

    首先请允许我吐槽一下这个题面 这个题面透露出血腥与暴力,电影院里还藏汽油 所以情侣们,要是想看电影就在家里看吧 毕竟出来容易被烧 在家里看虽然观影效果不如在电影院里 但是, 起码咱生命安全啥的有保障啊 ...

  5. [LeetCode] 731. My Calendar II 我的日历之二

    Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event w ...

  6. [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  7. 关于Dev-C++用户所必须知道的知识

    开启这几个警告选项,才能拥有GCC编译器的较为全面的报错和警告 另外,目前Dev-C早已停止更新(停滞在版本号5.1,内置的GCC为4.9版本),且使用其检测数组变量也挺麻烦的,实在不推荐使用.

  8. scss 入门基础

    在一个项目中,样式是必不可少的一部分,而对于一个完整的项目来说是有个基准色调的.在项目需求变化不大的情况下,可以直接在css中写这些颜色值之类的东西.但是如果遇到一个朝令夕改的领导或者甲方,那会变得相 ...

  9. 第01组 Beta冲刺(1/5)

    队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11985569.html 作业博客: https://edu.cnblogs.com/campus/fz ...

  10. 在 Vue 中使用 装饰器 Decorator

    Decorator 的语法还没有通过提案,所以项目中很少用.不过最近刚好有一个需求用到了. 装饰器的语法 http://es6.ruanyifeng.com/#docs/decorator 需求是,有 ...