Nginx高級配置-壓縮功能


              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  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
   http {
     include       mime.types;
       
     default_type  text/html;
       
     charset utf-8;
   
     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的值發生改變時,並不會使用本地緩存啦,而是會發送新的請求,獲取文件的最新狀態。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM