Apache的压缩与缓存-----网页优化
一、Apache网页的压缩
定义:对apache服务器发布的网页内容进行压缩后再发送到客户端的浏览器。减少了网络传输用时,也加快了网页加载的速度。
mod_gzip 模块与mod_deflate 模块
1.两者均使用gzip压缩算法,运作原理类似
2.mod_deflate 压缩速度略快,而mod_gzip 的压缩比略高
3.mod_gzip 对服务器CPU的占用要高- -些
4.高流量的服务器,使用mod_deflate 可能会比mod_gzip 加载速
度更快
实验走起
1、代码步骤如下:
[ ~]# mkdir /aaa [ ~]# mount.cifs //192.168.10.12/rpm /aaa Password for //192.168.10.12/rpm: [ ~]# cd /aaa [ aaa]# ls LAMP [ aaa]# cd LAMP/ [ 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 [ LAMP]# tar jxvf httpd-2.4.29.tar.bz2 -C /opt [ LAMP]# tar zxvf apr-1.6.2.tar.gz -C /opt/ [ LAMP]# tar zxvf apr-util-1.6.0.tar.gz -C /opt [ LAMP]# cd /opt/ [ opt]# ls apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh [ opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr [ opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util [ opt]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel -y [ opt]# cd httpd-2.4.29/ [ 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 [ httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi [ httpd-2.4.29]# make [ httpd-2.4.29]# make install [ httpd-2.4.29]# cd /usr/local/httpd/ [ httpd]# ls bin cgi-bin error icons lib man modules build conf htdocs include logs manual [ httpd]# cd conf [ conf]# ls extra httpd.conf magic mime.types original [ conf]# vim httpd.conf [ conf]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf [ 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保存退出 [ conf]# /usr/local/httpd/bin/apachectl -t Syntax OK //验证语法是否正确 [ conf]# /usr/local/httpd/bin/apachectl start [ conf]# netstat -ntap | grep 80 tcp 0 0 192.168.234.172:80 0.0.0.0:* LISTEN 82881/httpd [ conf]# ls extra httpd.conf magic mime.types original [ conf]# cd .. [ httpd]# ls bin cgi-bin error icons lib man modules build conf htdocs include logs manual [ httpd]# cd htdocs/ [ htdocs]# ls index.html [ htdocs]# cat index.html <html><body><h1>It works!</h1></body></html> [ htdocs]# cd /usr/local/httpd/bin/ [ bin]# ./apachectl -t -D DUMP_MODULES | grep "deflate" deflate_module (shared)
2、在/htdocs中添加进图片jpg,在index.html中输入<img src=“time.jpg”/>,客户端通过IP访问浏览器,用fiddler进行捕捉。
[ bin]# cd /aaa/LAMP [ 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 [ LAMP]# cp lf.jpg /usr/local/httpd/htdocs/ //把图片复制到对应的文件夹 [ LAMP]# cd /usr/local/httpd/ [ httpd]# cd htdocs/ [ htdocs]# ls index.html lf.jpg [ htdocs]# vim index.html <html><body><h1>It works!</h1> <img src="time.jpg"/> </body></html> //格式做以上更改,在当中插入图片,按Esc退出,输入:wq保存退出
结论:我们只要在网页中嵌入了图片或者视频这类格式文件时,网页会通过打压缩的方式再进行传输,传到对方的客户端之后在进行解压缩的识别
二、Apache网页的缓存
定义:将一部分经常不会变动或变动较少的页面缓存。下次浏览器访问这些页面时,不需要再次下载。提高了用户的访问速度,降低客户端的访问频率。
基于之前已经对apache的配置文件进行了相对应的设置,所以我们接下来只需要做如下更改:
1、代码如下:
[ htdocs]# vim /etc/httpd.conf 在末行按o转下行插入以下内容 <IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 50 seconds" </IfModule> //修改完成后按Esc退出,输入:wq保存退出 [ htdocs]# pwd /usr/local/httpd/htdocs [ htdocs]# cd /usr/local/httpd/ [ httpd]# cd bin/ [ bin]# ./apachectl -t //检查语法 Syntax OK [ bin]# ./apachectl stop [ bin]# ./apachectl start
2、验证,进行抓包测试
相关推荐
wys 2020-06-10
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03