Apache的网页优化和安全优化
Apache网页优化概述
■在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言之默认配置是针对以前较低的服务器配置的,以前的配置已经不适用当今互联网时代
■为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache优化的内容
■优化内容
1、配置网页压缩功能
2、配置网页缓存
3、工作模式的选择与参数优化
4、配置隐藏版本号
5、配置防盗链
一、网页压缩
gzip介绍
■配置Apache的网页压缩功能,是使用gzip压缩算法来对网页内容进行压缩后再传输到客户端浏览器
■作用
●降低了网络传输的字节数,加快网页加载的速度
●节省流量,改善用户的浏览体验
●gzip与搜索引擎的抓取工具有着更好的关系
Apache的压缩模块
■Apache实现网页压缩的功能模块包括
●mod_gzip 模块
●mod_deflate模块
■Apache 1.x
●没有内建网页压缩技术,但可使用第3三方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加载速度更快
启用网页压缩功能步骤:
1.检查是否安装mod_deflate模块
apachectl -t -D DUMP_MODULES | grep "deflate"
2.如果没有安装mod_ deflate 模块,重新编译安装Apache添加mod_deflate模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak
cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate #加入mod_deflate 模块
make && make install
3.配置mod_deflate 模块启用
vim /usr/local/httpd/conf/httpd.conf
--52行--修改
Listen 192.198.28.10:80
- --105行---取消注释
LoadModule deflate_module modules/mod_deflate. so #开启mod_deflate模块
--197行--取消注释,修改
ServerName www.kgc.com:80
--末行添加--
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript text/jpg text/png #代表对什么样的内容启用gzip压缩
DeflateCompressionLevel 9 #代表压缩级别,范围为1~9,企业服务器一般设置为为6
SetOutputFilter DEFLATE #代表启用deflate模块对本站点的输出进行gzip压缩
</IfModule>
4.检查安装情况,启动服务
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "deflate" #检查mod_deflate模块是否已安装
deflate module (shared) #出现这一行表示已安装的正确结果
systemctl start httpd.service
5.测试mod_deflate 压缩是否生效
cd /usr/local/httpd/htdocs
先将123.jpg(你想要上传的图片名)文件传到/usr/local/httpd/htdocs日录下
vim index.html
<html><body><h1> It works!</h1> #这是要在网页输入的内容,内容自己定
<img src="123.jpg"/> #这里的123.jpg要和/usr/local/httpd/htdocs下面的名字一致
</body></html>
检测:
方法一:
在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络--->选择HTML、WS、其他
访问http://192.168.28.10,双击200响应消息查看响应头中包含Content-Encoding:gzip
方法二:
在Windows7系统中依次安装Microsoft. NET4和fiddler 软件(windows10中只需要安装fiddler),打开fiddler软件
选择inspectors ---> 选择Headers
浏览器访问http://192.168.28.10 ,双击200响应消息查看Content-Encoding:gzip
在Linux系统中检测:
在Windows10系统中检测:
二、网页缓存
配置网页的缓存时间
■通过mod_expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求
■启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,客户端浏览器根据标签决定下次访问是在本地机器的缓存中获取页面不需要向服务器再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
启用网页缓存功能步骤:
1.检查是否安装mod_expires 模块
apachectl -t -D DUMP_MODULES | grep “expires”
2.如果没有安装mod_expires模块,重新编译安装Apache添加mod_expires模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak1
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-expires \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires #加入mod_expires 模块
make && make install
3.配置mod expires 模块启用
vim /usr/local/httpd/conf/httpd.conf
-- 52行--修改
Listen 192.198.28.10:80
--111行--取消注释
LoadModule expires_module modules/mod_expires.so #开启mod_expires 模块
--199行--取消注释,修改
ServerName www.kgc.com:80
--末行添加--
<IfModule mod_expires.c>
ExpiresActive On #打开网页缓存功能
ExpiresDefault "access plus 60 seconds" #设置缓存60秒
</IfModule>
4.检查安装情况,启动服务
apachectl -t #验证配置文件的配置是否正确
apachectl -t -D DUMP_MODULES | grep "expires" #检查mod_ deflate模块是否已安装
deflate module (shared) #出现这个表示已安装的正确结果
systemctl start httpd.service
5.测试缓存是否生效
cat /usr/local/httpd/htdocs/index.html
方法一:
在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络--->选择HTML、WS、其他
访问http://192.168.28.10 ,双击200消息查看响应头中包含Expires项
方法二:
在Windows系统中依次安装Microsoft.NET4和fiddler软件,打开fiddler软件
选择inspectors ---> 选择Headers
浏览器访问http://192.168.28.10,双击200消息查看Expires项
在Linux系统中测试缓存
在Windows系统中测试缓存:
三、配置Apache隐藏版本信息
■Apache的版本信息,透露了一定的漏洞信息,从而给网站带来安全隐患
■生产环境中要配置 Apache隐藏版本信息
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,只显示名称,没有版本
ServerSignature Off
#ServerTokens 表示 Server回送给客户端的响应头域是否包含关于服务器OS类型和编译过的模块描述信息。
systemctl start httpd.service
浏览器访问http://192.168.28.10,双击200消息查看Server项
四、配置Apache实现防盗链
■防盗链是防止别人的网站代码里面盗用我们自己服务器上的图片、文件、视频等相关资源
■如果别人盗用网站的这些静态资源,明显的是会增大服务器的带宽压力
■作为网站的维护人员,要杜绝服务器的静态资源被其他网站盗用
1.检查是否安装mod rewrite模块
apachectl -t -D DUMP_MODULES | grep “rewrite”
2.如果没有安装mod_ rewrite 模块,重新编译安装Apache 添加mod_ rewrite模块
systemctl stop httpd.service
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak2
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
cd /opt/httpd-2.4.29/
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \ #加入mod_rewrite模块
--enable-charset-lite \
--enable-cgi \
--enable-deflate \
--enable-expires
make && make install
3.配置mod_ _rewrite 模块启用
vim /usr/local/httpd/conf/httpd.conf
--157行--取消注释
LoadModule rewrite_module modules/mod_rewrite.so
--224行--
<Directory "/usr/local/httpd/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
RewriteEngine On #打开rewrite 功能,加入mode_rewrite 模块内容
RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC] #设置匹配规则
RewriteCond %{HTTP_REFERER} !^http://kgc.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http: I /www.kgc.com/$ [NC]
RewriteRule .*\.(gif | jpg | swf)$ http://www.kgc.com/error.png #设置跳转动作
</Directory>
RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]的字段含义:
“%{HTTPREFERER}":存放一个链接的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”:表示转发到这个路径。
整个配置的含义是使用本网站以外的网站域名访问本站的图片文件时,显示error.png 这个图片。
4.网页准备
Web源主机配置:
cd /usr/local/httpd/htdocs
将game.jpg、error.png文件传到/usr/local/httpd/htdocs目录下
vim index.html
<html><body><h1>this is kgc.com!</h1>
<img src="game.jpg"/>
</body></html>
echo "192.168.28.10 www.kgc.com" >> /etc/hosts
echo "192.168.28.7 www.benet.com" >> /etc/hosts
盗链网站主机
cd /usr/local/httpd/htdocs #yum安装的httpd服务的默认路径为/var/www/html/
vim index.html
<html><body><h1>this is benet.com!</h1>
<img src= "http://www.kgc.com/game.jpg"/>
</body></html>
echo "192.168.28.10 www.kgc.com" >> /etc/hosts
echo "192.168.28.7 www.benet.com" >> /etc/hosts
5.在盗图网站主机_上进行浏览器验证
http://www.benet.com