Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度

         Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度

                                                作者:尹正杰

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

 

 

 

一.mod_deflate模块概述

mod_deflate模块功能:
    压缩页面从而优化传输速度

mod_deflate模块适用场景:
  (1)节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持
  (2)压缩适于压缩的资源,例如文本文件
      LoadModule deflate_module modules/mod_deflate.so SetOutputFilter DEFLATE
      SetOutputFilter DEFLATE
      # Restrict compression to these MIME types
      AddOutputFilterByType DEFLATE text/plain
      AddOutputFilterByType DEFLATE text/html
      AddOutputFilterByType DEFLATE application/xhtml+xml
      AddOutputFilterByType DEFLATE text/xml
      AddOutputFilterByType DEFLATE application/xml
      AddOutputFilterByType DEFLATE application/x-javascript
      AddOutputFilterByType DEFLATE text/javascript
      AddOutputFilterByType DEFLATE text/css
  (3)Level of compression (支持的压缩级别Highest 9 - Lowest 1)
      DeflateCompressionLevel 9
  (4)排除特定旧版本的浏览器,不支持压缩
      Netscape 4.x 只压缩text/html
        BrowserMatch ^Mozilla/4 gzip-only-text/html
      Netscape 4.06-08三个版本 不压缩
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
      Internet Explorer标识本身为"Mozilla / 4",但实际上是能够处理请求的压缩。如果用户代理首部匹配字符串“MSIE”(“B”为单词边界”),就关闭之前定义的限制
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
 
博主推荐阅读:
  http://httpd.apache.org/docs/2.4/mod/mod_deflate.html

Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度_第1张图片

 

 

 

二.准备测试数据

1>.创建测试文件

[[email protected] ~]# ll -h /var/log/messages
-rw-r--r-- 1 root root 119K Dec  9 17:10 /var/log/messages
[[email protected] ~]# 
[[email protected] ~]# touch /var/www/html/asite/log.txt
[[email protected] ~]# 
[[email protected] ~]# ll -h /var/www/html/asite/log.txt 
-rw-r--r-- 1 root root 0 Dec  9 17:12 /var/www/html/asite/log.txt
[[email protected] ~]# 
[[email protected] ~]# for _ in  `seq 1000`; do cat /var/log/messages >> /var/www/html/asite/log.txt;done      #我们将"/var/log/message"内容遍历100次写入到"/var/www/html/asite/log.txt"文件中
[[email protected] ~]# 
[[email protected] ~]# ll -h /var/www/html/asite/log.txt     #很明显,文件相比"/var/log/message"要打了1000倍。
-rw-r--r-- 1 root root 117M Dec  9 17:13 /var/www/html/asite/log.txt
[[email protected] ~]# 
[[email protected] ~]# 

2>.编写httpd服务端的配置

[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %{%Y-%m-%d %H:%M:%S}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" testlog


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf      #查看主配置文件内容
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep ServerRoot
ServerRoot "/etc/httpd"
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep IncludeOptional
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# cat /etc/httpd/conf.d/virtualHost.conf 

    DocumentRoot "/var/www/html/asite"
    ServerName "www.a.com"        #别忘记在这里写上相应的虚拟主机的名称哟~以下配置类似修改即可。
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_asite_log" testlog




    DocumentRoot "/var/www/html/bsite"
    ServerName "www.b.org"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_bsite_log" testlog




    DocumentRoot "/var/www/html/csite"
    ServerName "www.c.net"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_csite_log" testlog

[[email protected] ~]# 
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# 
[[email protected] ~]# systemctl reload httpd
[[email protected] ~]# 
[[email protected] ~]# ss -ntl
State       Recv-Q Send-Q             Local Address:Port                            Peer Address:Port              
LISTEN      0      128                            *:80                                         *:*                  
LISTEN      0      128                            *:22                                         *:*                  
LISTEN      0      128                           :::22                                        :::*                  
[[email protected] ~]#

3>.验证客户端是否可以正常访问"log.txt"

Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度_第2张图片

 

三.配置虚拟主机压缩案例(生产环境中强烈推荐启用压缩功能,尤其是把大文件上传到CDN节点时,如果你不压缩文件,你就会浪费带宽,而这个带宽成本的浪费实际上是你们公司来买单!)

1>.编辑httpd服务器的配置文件

[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %{%Y-%m-%d %H:%M:%S}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" testlog
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    
    CustomLog "logs/access_log" testlog


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

EnableSendfile on
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf      #查看主配置文件内容
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep ServerRoot
ServerRoot "/etc/httpd"
[[email protected] ~]# 
[[email protected] ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf  | grep IncludeOptional
IncludeOptional conf.d/*.conf
[[email protected] ~]# 
[[email protected] ~]# cat /etc/httpd/conf.d/virtualHost.conf 

    DocumentRoot "/var/www/html/asite"
    ServerName "www.a.com"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_asite_log" testlog
    AddOutputFilterByType DEFLATE text/plain      #压缩文本文件(text),以"*.txt"结尾的文本文件,比如"log.txt"
    AddOutputFilterByType DEFLATE text/html       #压缩文本文件(text),以"*.html"结尾的超文本文件,比如"index.html"
    DeflateCompressionLevel 9




    DocumentRoot "/var/www/html/bsite"
    ServerName "www.b.org"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_bsite_log" testlog




    DocumentRoot "/var/www/html/csite"
    ServerName "www.c.net"
    
        Require all granted
    
    CustomLog "/var/log/httpd/access_csite_log" testlog

[[email protected] ~]# 
[[email protected] ~]# httpd -t
Syntax OK
[[email protected] ~]# 
[[email protected] ~]# systemctl reload httpd
[[email protected] ~]# 

2>.验证客户端是否支持压缩了

 Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度_第3张图片

 

你可能感兴趣的:(Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度)