一、Apache网页的压缩
定义:对apache服务器发布的网页内容进行压缩后再发送到客户端的浏览器。减少了网络传输用时,也加快了网页加载的速度。
mod_gzip 模块与mod_deflate 模块
1.两者均使用gzip压缩算法,运作原理类似
2.mod_deflate 压缩速度略快,而mod_gzip 的压缩比略高
3.mod_gzip 对服务器cpu的占用要高- -些
4.高流量的服务器,使用mod_deflate 可能会比mod_gzip 加载速
度更快
实验走起
1、代码步骤如下:
[root@localhost ~]# mkdir /aaa
[root@localhost ~]# mount.cifs //192.168.10.12/rpm /aaa
Password for root@//192.168.10.12/rpm:
[root@localhost ~]# cd /aaa
[root@localhost aaa]# ls
LAMP
[root@localhost aaa]# cd LAMP/
[root@localhost LAMP]# ls
apr-1.6.2.tar.gz error.png
apr-util-1.6.0.tar.gz ha.jpg
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2
cronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gz
discuz_X2.5_SC_UTF8.zip PHP-5.6.11.tar.bz2
[root@localhost LAMP]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt
[root@localhost LAMP]# tar zxvf apr-1.6.2.tar.gz -C /opt/
[root@localhost LAMP]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost LAMP]# cd /opt/
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel -y
[root@localhost opt]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]# ls
ABOUT_APACHE CMakeLists.txt INSTALL NWGNUmakefile
acinclude.m4 config.layout InstallBin.dsp os
Apache-apr2.dsw configure LAYOUT README
Apache.dsw configure.in libhttpd.dep README.cmake
apache_probes.d docs libhttpd.dsp README.platforms
ap.d emacs-style libhttpd.mak ROADMAP
build httpd.dep LICENSE server
BuildAll.dsp httpd.dsp Makefile.in srclib
BuildBin.dsp httpd.mak Makefile.win support
buildconf httpd.spec modules test
CHANGES include NOTICE VERSIONING
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
[root@localhost httpd-2.4.29]# make
[root@localhost httpd-2.4.29]# make install
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/
[root@localhost httpd]# ls
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# vim httpd.conf
[root@localhost conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost conf]# vim /etc/httpd.conf
/deflate,搜索这个关键词
LoadModule deflate_module modules/mod_deflate.so //此行去除注释
/headers,搜索这个关键词
LoadModule headers_module modules/mod_headers.so //此行需要去除注释
/filter,搜索这个关键词
LoadModule filter_module modules/mod_filter.so //此行需要去除注释
/Listen,搜索这个关键词
Listen 192.168.234.172:80
#Listen 80
/ServerName,搜索这个关键词
ServerName www.kgc.com:80
按大写字母G到末行,按o转下行插入
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/ipg text/png
DeflateCompressionLevel 9
SetoutputFilter DEFLATE
</IfModule>
//修改结束后按Esc退出,输入:wq保存退出
[root@localhost conf]# /usr/local/httpd/bin/apachectl -t
Syntax OK
//验证语法是否正确
[root@localhost conf]# /usr/local/httpd/bin/apachectl start
[root@localhost conf]# netstat -ntap | grep 80
tcp 0 0 192.168.234.172:80 0.0.0.0:* LISTEN 82881/httpd [root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# cd ..
[root@localhost httpd]# ls
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
[root@localhost httpd]# cd htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# cat index.html
<html><body><h2>It works!</h2></body></html>
[root@localhost htdocs]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate"
deflate_module (shared)
2、在/htdocs中添加进图片jpg,在index.html中输入<img src=“time.jpg”/>,客户端通过IP访问浏览器,用fiddler进行捕捉。
[root@localhost bin]# cd /aaa/LAMP
[root@localhost LAMP]# ls
apr-1.6.2.tar.gz ha.jpg
apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
awstats-7.6.tar.gz lf.jpg
cronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gz
discuz_X2.5_SC_UTF8.zip PHP-5.6.11.tar.bz2
error.png
[root@localhost LAMP]# cp lf.jpg /usr/local/httpd/htdocs/ //把图片复制到对应的文件夹
[root@localhost LAMP]# cd /usr/local/httpd/
[root@localhost httpd]# cd htdocs/
[root@localhost htdocs]# ls
index.html lf.jpg
[root@localhost htdocs]# vim index.html
<html><body><h2>It works!</h2>
<img src="time.jpg"/>
</body></html>
//格式做以上更改,在当中插入图片,按Esc退出,输入:wq保存退出
结论:我们只要在网页中嵌入了图片或者视频这类格式文件时,网页会通过打压缩的方式再进行传输,传到对方的客户端之后在进行解压缩的识别
二、Apache网页的缓存
定义:将一部分经常不会变动或变动较少的页面缓存。下次浏览器访问这些页面时,不需要再次下载。提高了用户的访问速度,降低客户端的访问频率。
基于之前已经对apache的配置文件进行了相对应的设置,所以我们接下来只需要做如下更改:
1、代码如下:
[root@localhost htdocs]# vim /etc/httpd.conf
在末行按o转下行插入以下内容
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 50 seconds"
</IfModule>
//修改完成后按Esc退出,输入:wq保存退出
[root@localhost htdocs]# pwd
/usr/local/httpd/htdocs
[root@localhost htdocs]# cd /usr/local/httpd/
[root@localhost httpd]# cd bin/
[root@localhost bin]# ./apachectl -t //检查语法
Syntax OK
[root@localhost bin]# ./apachectl stop
[root@localhost bin]# ./apachectl start
2、验证,进行抓包测试