4.Apache网页优化

文章目录

  • Apache网页优化
    • 网页压缩
    • 网页缓存
    • 隐藏版本信息
    • Apache防盗链

Apache网页优化

  • Apache网页优化

    • 网页压缩
    • 网页缓存
  • Apache安全优化

    • 隐藏版本信息
    • 配置防盗链

网页压缩

  • 配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后再传输到客户端浏览器

  • 作用

    • 降低了网络传输的字节数,加快网页加载的速度
    • 节省流量,改善用户的浏览体验
    • gzip与搜索引擎的抓取工具有着更好的关系
  • Apache实现网页压缩的功能模块包括

    • mod_ gzip 模块
    • mod_ deflate 模块
  • Apache 1.x

    • 没有内建网页压缩技术,但可使用第三方mod_ _gzip 模块执行压缩
  • Apache 2.x

    • 在开发的时候,内建了mod_ deflate 这个模块,取代mod_ gzip
  • mod_gzip 模块与mod_deflate 模块

    • 两者均使用gzip压缩算法,运作原理类似
    • mod_deflate压缩速度略快, 而mod_gzip的压缩比略高_
    • mod_gzip对服务器CPU的占用要高一些
    • 高流量的服务器使用mod_deflate可能会比mod_gzip 加载速度更快
apachectl -t -D DUMP_MODULES | grep "deflate"       ##查看是否有相关模块
systemctl stop httpd
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak

./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate	

make -j2
make install
vim /usr/local/httpd/conf/httpd.conf


--52行--修改
Listen 192.198.80.10:80


--199行--修改
ServerName www.ggl.com:80

--105行--取消注释
LoadModule deflate_module modules/mod_deflate.so		#开启mod_deflate 模块





--末行添加--


AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png					#代表对什么样的内容启用gzip压缩
DeflateCompressionLevel 9			#代表压缩级别,范围为1~9
SetOutputFilter DEFLATE				#代表启用deflate 模块对本站点的输出进行gzip压缩




AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png					
DeflateCompressionLevel 9			
SetOutputFilter DEFLATE				

apachectl -t			                            #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate"		#检查 mod_deflate 模块是否已安装
  deflate_module (shared)							#已安装的正确结果

systemctl start httpd
5.测试 mod_deflate 压缩是否生效
cd /usr/local/httpd/htdocs

先将game.jpg文件传到/usr/local/httpd/htdocs目录下
vim index.html

It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!It works!

4.Apache网页优化_第1张图片

网页缓存

  • 通过mod_ expire模块配置 Apache,使网页能在客户端浏览器缓存一段时间, 以避免重复请求
  • 启用mod_ expire模块后, 会自动生成页面头部信息中的Expires标签和Cache-Control标签,客户端浏览器根据标签决定I下次访问是在本地机器的缓存中获取页面不需要向服务器再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
apachectl -t -D DUMP_MODULES | grep "expires"

检查是否安装 mod_expires 模块
systemctl stop httpd
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak1


yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel


./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires		


make -j2 
make install
vim /usr/local/httpd/conf/httpd.conf

--52行--修改
Listen 192.198.80.10:80

--111行--取消注释
LoadModule expires_module modules/mod_expires.so		#开启mod_expires 模块

--199行--取消注释,修改
ServerName www.ggl.com:80

--末行添加--


  ExpiresActive On								#打开网页缓存功能
  ExpiresDefault "access plus 60 seconds"		#设置缓存60秒





  ExpiresActive On								
  ExpiresDefault "access plus 3600 seconds"		

apachectl -t			                           #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires"		#检查 mod_deflate 模块是否已安装
  deflate_module (shared)							#已安装的正确结果

systemctl start httpd

4.Apache网页优化_第2张图片

隐藏版本信息

4.Apache网页优化_第3张图片

vim /usr/local/httpd/conf/httpd.conf

--491行--取消注释
Include conf/extra/httpd-default.conf
vim /usr/local/httpd/conf/extra/httpd-default.conf

--55行--修改
ServerTokens Prod            #将原本的 Full 改为 Prod,只显示名称,没有版本
#ServerTokens 表示 Server 回送给客户端的响应头域是否包含关于服务器 OS 类型和编译过的模块描述信息。
systemctl restart httpd

4.Apache网页优化_第4张图片

Apache防盗链

##检查是否安装 mod_rewrite 模块

apachectl -t -D DUMP_MODULES | grep "rewrite"
##配置 mod_rewrite 模块启用
vim /usr/local/httpd/conf/httpd.conf


--157行--取消注释
LoadModule rewrite_module modules/mod_rewrite.so


--224行--

  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted

  RewriteEngine On 						#打开 rewrite 功能,加入 mode_rewrite 模块内容
  RewriteCond %{HTTP_REFERER} !^http://ggl.com/.*$ [NC]				#设置匹配规则
  RewriteCond %{HTTP_REFERER} !^http://ggl.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/$ [NC]
  RewriteRule .*\.(gif|jpg|swf)$ http://www.ggl.com/error.png		#设置跳转动作








  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted

  RewriteEngine On 						
  RewriteCond %{HTTP_REFERER} !^http://ggl.com/.*$ [NC]				
  RewriteCond %{HTTP_REFERER} !^http://ggl.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.ggl.com/$ [NC]
  RewriteRule .*\.(gif|jpg|swf)$ http://www.ggl.com/error.png	


命令解释

RewriteCond % {HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]  的字段含义:

“%{HTTP_REFERER}”  :存放一个链接的 URL,表示从哪个链接中转访问目录下的静态资源。
“!^” :表示不以后面的字符串开头。
“http://www.kgc.com” :是本网站的路径,按整个字符串匹配。
“.*$” :表示以任意字符结尾。
“[NC]” :表示不区分大小写字母。



RewriteRule .*\.(gif|jpg|swf)$ http://www.kgc.com/error.png  的字段含义:
“.” :表示匹配一个字符。
“*” :表示匹配 0 到多个字符,与“.”合起来的意思是匹配 0 到多次前面的任意字符,如果是 1 到多次匹配可以用“+”表示。
“\.” :在这里的“\”是转义符,“\.”就代表符号“.”的意思。因为“.”在指令中是属于规则字符,有相应的含义, 如果需要匹配,需要在前面加个转义符“\”,其它规则字符如果需要匹配,也做同样处理。
“(gif|jpg|swf)” :表示匹配“gif”、“jpg”、“swf”任意一个,“$”表示结束。最后的规则是以“.gif”、“.jpg”、“.swf”结尾, 前面是1到多个字符的字符串,也就是匹配图片类型的文件。
“http://www.kgc.com/error.png” :表示转发到这个路径 。

4.Apache网页优化_第5张图片

你可能感兴趣的:(6.Web服务和HTTP,Nginx,apache,服务器,搜索引擎)