Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度
Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.mod_deflate模块概述
mod_deflate模块功能:
压缩页面从而优化传输速度 mod_deflate模块适用场景:
()节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持
()压缩适于压缩的资源,例如文本文件
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
()Level of compression (支持的压缩级别Highest - Lowest )
DeflateCompressionLevel
()排除特定旧版本的浏览器,不支持压缩
Netscape .x 只压缩text/html
BrowserMatch ^Mozilla/ gzip-only-text/html
Netscape 4.06-08三个版本 不压缩
BrowserMatch ^Mozilla/\.[] 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
二.准备测试数据
1>.创建测试文件
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/log/messages
-rw-r--r-- root root 119K Dec : /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# touch /var/www/html/asite/log.txt
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/www/html/asite/log.txt
-rw-r--r-- root root Dec : /var/www/html/asite/log.txt
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# for _ in `seq `; do cat /var/log/messages >> /var/www/html/asite/log.txt;done #我们将"/var/log/message"内容遍历100次写入到"/var/www/html/asite/log.txt"文件中
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/www/html/asite/log.txt #很明显,文件相比"/var/log/message"要打了1000倍。
-rw-r--r-- root root 117M Dec : /var/www/html/asite/log.txt
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.编写httpd服务端的配置
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
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
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" testlog
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf #查看主配置文件内容
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf | grep ServerRoot
ServerRoot "/etc/httpd"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf | grep IncludeOptional
IncludeOptional conf.d/*.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/httpd/conf.d/virtualHost.conf
<VirtualHost "*:80">
DocumentRoot "/var/www/html/asite"
ServerName "www.a.com" #别忘记在这里写上相应的虚拟主机的名称哟~以下配置类似修改即可。
<Directory "/var/www/html/asite">
Require all granted
</Directory>
CustomLog "/var/log/httpd/access_asite_log" testlog
</VirtualHost> <VirtualHost "*:80">
DocumentRoot "/var/www/html/bsite"
ServerName "www.b.org"
<Directory "/var/www/html/bsite">
Require all granted
</Directory>
CustomLog "/var/log/httpd/access_bsite_log" testlog
</VirtualHost> <VirtualHost "*:80">
DocumentRoot "/var/www/html/csite"
ServerName "www.c.net"
<Directory "/var/www/html/csite">
Require all granted
</Directory>
CustomLog "/var/log/httpd/access_csite_log" testlog
</VirtualHost>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# httpd -t
Syntax OK
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl reload httpd
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# 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 :::*
[root@node101.yinzhengjie.org.cn ~]#
3>.验证客户端是否可以正常访问"log.txt"
三.配置虚拟主机压缩案例(生产环境中强烈推荐启用压缩功能,尤其是把大文件上传到CDN节点时,如果你不压缩文件,你就会浪费带宽,而这个带宽成本的浪费实际上是你们公司来买单!)
1>.编辑httpd服务器的配置文件
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
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
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" testlog
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf #查看主配置文件内容
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf | grep ServerRoot
ServerRoot "/etc/httpd"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/httpd/conf/httpd.conf | grep IncludeOptional
IncludeOptional conf.d/*.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/httpd/conf.d/virtualHost.conf
<VirtualHost "*:80">
DocumentRoot "/var/www/html/asite"
ServerName "www.a.com"
<Directory "/var/www/html/asite">
Require all granted
</Directory>
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
</VirtualHost> <VirtualHost "*:80">
DocumentRoot "/var/www/html/bsite"
ServerName "www.b.org"
<Directory "/var/www/html/bsite">
Require all granted
</Directory>
CustomLog "/var/log/httpd/access_bsite_log" testlog
</VirtualHost> <VirtualHost "*:80">
DocumentRoot "/var/www/html/csite"
ServerName "www.c.net"
<Directory "/var/www/html/csite">
Require all granted
</Directory>
CustomLog "/var/log/httpd/access_csite_log" testlog
</VirtualHost>
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# httpd -t
Syntax OK
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# systemctl reload httpd
[root@node101.yinzhengjie.org.cn ~]#
2>.验证客户端是否支持压缩了
Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度的更多相关文章
- 使用mod_deflate模块压缩页面优化传输速度
在HTTPD主配置文件中添加如下,并确保deflate模块是启用的 #vim /etc/httpd/conf/httpd.conf SetOutputFilter DEFLATE//调用一个叫DEFL ...
- Apache使用mod_deflate模块压缩页面优化传输速度
可以写为一行,也可以写多行,默认为gzip SetOutputFilter DEFLATE # Restrict compression to these MIME types AddOutputFi ...
- Httpd服务入门知识-Httpd服务常见配置案例之MPM( Multi-Processing Module)多路处理模块
Httpd服务入门知识-Httpd服务常见配置案例之MPM( Multi-Processing Module)多路处理模块 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.M ...
- Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置
Httpd服务入门知识-Httpd服务常见配置案例之DSO( Dynamic Shared Object)加载动态模块配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.加载动 ...
- Httpd服务入门知识-Httpd服务安装
Httpd服务入门知识-Httpd服务安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Httpd概述 1>.Httpd介绍 20世纪90年代初,国家超级计算机应用中心 ...
- Httpd服务入门知识-https(http over ssl)安全配置
Httpd服务入门知识-https(http over ssl)安全配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SSL会话的简化过程 ()客户端发送可供选择的加密方式, ...
- Httpd服务入门知识-Httpd服务常见配置案例之Apache的工作做状态status页面
Httpd服务入门知识-Httpd服务常见配置案例之Apache的工作做状态status页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.status功能概述 status页 ...
- Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享
Httpd服务入门知识-Httpd服务常见配置案例之实现用户家目录的http共享 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.实现用户家目录的http共享前提 在配置家目录共 ...
- Httpd服务入门知识-Httpd服务常见配置案例之基于用户账号实现访问控制
Httpd服务入门知识-Httpd服务常见配置案例之基于用户账号实现访问控制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.基于用户的访问控制概述 认证质询: WWW-Auth ...
随机推荐
- 【Codeforces】B. Div Times Mod
B. Div Times Mod time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Mysql连接驱动与Java之间的版本不匹配问题(Mysql-connector-java与Java、Mysql版本对应关系)
一.问题如下: 我使用的是jdk12.0.1,jdk12在使用kettle时找不到能匹配的驱动版本来连接mysql: 我尝试过很多mysql连接驱动版本都直接报错,于是我将jdk版本降到了jdk8,结 ...
- Spring Boot 构造器参数绑定,越来越强大了!
在之前的文章:Spring Boot读取配置的几种方式,我介绍到 Spring Boot 中基于 Java Bean 的参数绑定,在一个 Java Bean 类上用 @ConfigurationPro ...
- webstorm创建js文件时自动生成js注释
设置webstorm创建js文件时自动生成js注释 settings--Editor--File and Code Temlates 黑色框框里的内容自己填写上去,以下是参考的代码块: /** * @ ...
- Alpha冲刺——总结篇
课程信息 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺 团队目标 切实可行的计算机协会维修预约平台 团队信息 队员学号 队员姓名 个人博客地址 备注 22 ...
- linux免费https证书申请教程
linux免费https证书申请教程直接去阿里云 菜单有个证书服务进去有个购买证书菜单 选择免费的 然后会提示写个人资料 然后系统生成csr 然后提交审核这个时候会有份邮件 文件下载上传到你的服务器 ...
- Linux crond任务调度(定时任务),Linux磁盘分区/挂载
一.crond任务调度 1.基本语法 crontab [选项] -e : 编辑 crontab定时任务 -l : 查询crontab -r : 删除当前用户所有的crontab任务 例子: 每分钟执行 ...
- eclipse从svn检出maven项目
使用Eclipse从svn检出项目. 打开Eclipse,在project explorer空白区域右键鼠标移至import选择import. 2 之后选择svn-->从svn检出项目,然后输入 ...
- springmvc集成shiro后,session、request是否发生变化
1. 疑问 我们在项目中使用了spring mvc作为MVC框架,shiro作为权限控制框架,在使用过程中慢慢地产生了下面几个疑惑,本篇文章将会带着疑问慢慢地解析shiro源码,从而解开心里面的那点小 ...
- 【SQL】各取所需 | SQL JOIN连接查询各种用法总结
前面 在实际应用中,大多的查询都是需要多表连接查询的,但很多初学SQL的小伙伴总对各种JOIN有些迷糊.回想一下,初期很长一段时间,我常用的似乎也就是等值连接 WHERE 后面加等号,对各种JOIN也 ...