Apache网页优化概述
●在企业中,部署Apache后只采用默认的配置参数,会
引发网站很多问题,换言之默认配置是针对以前较低的
服务器配置的,以前的配置已经不适用当今互联网时代
●为了适应企业需求,就需要考虑如何提升Apache的性
能与稳定性,这就是Apache优化的内容
◆优化内容与gzip介绍
配置网页压缩功能
配置网页缓存
工作模式的选择与参数优化
配置隐藏版本号
配置防盗链
…
◆gzip介绍
客户端浏览器配置Apache的网页压缩功能,是使用gzip压缩算法来
对网页内容进行压缩后再传输到
作用
降低了网络传输的字节数,加快网页加载的速度
节省流量,改善用户的浏览体验
gzip与搜索引擎的抓取工具有着更好的关系
◆Apache的压缩模块
■Apache实现网页压缩的功能模块包括
● mod_gzip 模块
● mod_deflate 模块
■Apache 1.x
● 没有内建网页压缩技术,但可使用第三方jmod_gzip模块执行压缩
■Apache 2.x
● 在开发的时候,内建了mod_deflate 这个模块,取代mod_gzip
■mod_ gzip模块与mod_deflate模块
●两者均使用gzip压缩算法,运作原理类似
●mod deflate压缩速度略快,而mod_gzip的压缩比略高
●mod gzip对服务器CPU的占用要高一些
●高流量的服务器,使用mod_deflate可能会比mod_gzip 加载速度更快
另外,从Apache 2.0.45开始, mod deflate模块可使用DeflateCompressionLevel指令来设置压缩级别。
该指令的值可为1至(压缩速度最快,最低的压缩质量) 9 (最慢的压缩速度,压缩率最高)之间的整数,其默认值为
6 (压缩速度和压缩质量较为平衡的值)这个简单的变化更是使得mod deflate可以轻松媲美mod-gzip的压缩
Apache优化实操
◆mod deflate模块开启
##先编译安装Apache,如果不会可以到我的博客查看
https://blog.csdn.net/weixin_48190891/article/details/108331287
(1)检查是否安装了mod deflate模块
[root@localhost ~]# apachectl -D DUMP MODULES | grep "deflate"
如果没有安装mod deflate模块
[root@localhost ~]# systemctl stop httpd
[root@localhost ~]# yum install -y zlib-devel
[root@localhost ~# cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29]#
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi--enable-cgid \
--enable-deflate \
--enable-expires
####enable-deflate加入mode_deflate模块###
[root@localhost httpd-2.4.29]#make && make install
(3)配置mod deflate模块启用
编译安装后, mod deflate模块需要在httpd.conf文件启用才能生效。
[root@localhost httpd-2.4.29]vi /usr/local/httpd/conf/httpd.conf
LoadModule deflate module modules/mod_deflate.so # 去掉#号并在下面添加以下内容
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript image/png image/jpg
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>
#第一行代表对什么样的内容启用gzip压缩,
#第二行代表压缩级别
#第三行代表启用deflate 模块对本站点的输出进行 gzip 压缩。
(4)检测httpd.conf语法并重启服务使配置生效
[root@localhost httpd-2.4.29]# httpd -t
Syntax OK
[root@localhost httpd-2.4.29]# systemctl restart httpd
######
把b照片用Xmanager Enterprise工具传入/usr/local/httpd/htdocs/目下
[root@localhost httpd-2.4.29]#cd /usr/local/httpd/htdocs/
<html>
<head>
<title>-压缩测试页-</title>
</head>
<body><h1>这是一个测试网页内容压缩的页面! ! This is test Page!! </h1>
<img src=b.jpg />
</body>
</html>
-----注意中文乱码问题解决方法如下 -----
vi /etc/httpd.conf
AddDefaultCharset utf-8
[root@localhost httpd-2.4.29]# systemctl restart httpd
###############用wireshaerk抓包测试图片传输是否压缩验证############
找到 HTTP/1.1 200 OK (JPEG JFIF image)
找到 Hyertext Transfer Protocol
找到 HTTP/1.1 200 OK \r\n
找到content-Encoding: gzip \r\n ###这个地方表示图片压缩########
########################################################
◆网页缓存
# 网页缓存是将一部分经常不会改变和变动很少的页面缓存,下次浏览器再次访问这些
# 页面时,不需要再次去下载这些页面,从而提高了用户的访问速度。
# Apache的mod expires模块会自动生成页面头部信息中的Expres标签和Cache-Control
# 标签,客户端浏览器根据标签决定下次访问是在本地机器的缓存中获取页面,不需要向服务器
# 再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的。
# 配置mod_expires模块的步骤与mod_deflate模块相似
(1)检查mod_expires模块是否安装
[root@localhost ~]# apachectl-D DUMP_MODULES | grep "expire"
如果没有安装 mod_expires 模块
[root@localhost ~]#systemctl stop httpd
[root@localhost ~]# yum install -y zlib-devel
[root@localhost ~]#cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29]#
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-cgid \
--enable-deflate \
--enable-expires
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# vi /etc/httpd.conf
LoadModule expires_module modules/mod_expires.so 去掉#号开启模块并在模块下添加以下内容
<lfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 60 seconds"
</IfModule>
(4) 检测httpd.conf 语法并启动服务
[root@localhost httpd-2.4.29]# httpd -t
Syntax OK
[root@localhost httpd-2.4.29]# systemctl start httpd
[root@localhost httpd-2.4.29]# systemctl restart httpd
(5) 重新检测模块是否安装
[root@localhost httpd-2.4.29]# apachectl -D DUMP_MODULES I grep "expires"
expires_module (shared)
################用wireshaerk抓包测试图片传输是否缓存验证##########
找到HTTP/1.1 200 OK (JPEG JFIF image)
找到Hyertext Transfer Protocol
找到HTTP/1.1 200 OK \r\n
找到Cache-Control :max-age=60 \r\n ###这个地方表示缓存60秒###
####################################################
◆隐藏版本信息
#一般情况下,软件的漏洞信息和特定版本是相关的。因此,
#软件的版本号对攻击者来说是很有价值的
[root@localhost ~]# vi /usr/local/httpd/conf/httpd.conf
Include conf/extra/httpd-default.conf 去掉前面的#
[root@localhost ~]# vi /usr/local/httpd/conf/extra/httpd-default.conf
ServerTokens Prod 把Full改为Prod
[root@localhost ~]# systemctl restart httpd
####抓包验证#######################
找到HTTP/1.1 200 OK (JPEG JFIF image)
找到Hyertext Transfer Protocol
找到HTTP/1.1 200 OK \r \n
找到Server: Apache \r\n ###这个地方的版本号看不见了 ###
#######################################################
◆配置防盗链
#http标准协议中有专门的Referer字段记录,它的作用如下。
1) 可以追溯上一个入站地址是什么。
2) 对于资源文件,可以跟踪到包含显示它的网页地址是什么,因此所有防盗链方法都
是基于这个Referer字段。
#####以下是在合法服务器上配置防盗链#####
###正常环境搭建###
20.0.0.25 www.51xit.top 做防盗链配置
20.0.0.26 做盗链网站
#首先20.0.0.25不做做防盗链配置
#之前我们已经上传过并访问过网页
#在20.0.0.26 仿站用到http://20.0.0.25/b.jpg这个图片
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable http
[root@localhost -]# vi /var/www/html/index.html
<html>
<head>
<title>--压缩测试页--</title>
</head>
<body><h1>盗图页面!! </h1>
<img src=http://20.0.0.25/b.jpg />
</body>
</html>
#去自己的电脑主页访问以下
#接下来做防盗链处理 20.0.0.25
(1)检查是否安装了mod_rewrite模块
apachectl -t -D DUMP_MODULES I grep "rewrite"
如果没有安装 mod_rewrite 模块
[root@localhost ~]#systemctl stop httpd
[root@localhost ~]# yum install -y zlib-devel
[root@localhost ~]#cd /opt/httpd-2.4.29/
[root@localhost httpd-2.4.29]#
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-cgid \
--enable-deflate \
--enable-expires
[root@localhost httpd-2.4.29]# make && make install
[root@localhost httpd-2.4.29]# systemctl start httpd
[root@localhost httpd-2.4.29]# systemctl restart httpd
[root@localhost ~] # vi /etc/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so 去掉#开启模块
[root@localhost ~] # vi /etc/httpd.conf
#在 AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
#下面添加:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://20.0.0.25/*
RewriteCond %{HTTP_REFERER} !^http://51xit.top/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://51xit.top$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.51xit.top/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.51xit.top$ [NC]
RewriteRule .*\.(gif|jpg|swf|png)$ https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1153343029,1355573270&fm=26&
gp=0.jpg [R,NC]
[root@localhost httpd-2.4.29]# systemctl restart httpd
上面最后的内容其中: https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1153343029,
1355573270&fm=26&gp=0.jpg 是在百度随便找的图片地址
#####验证 清除真机网页缓存,然后再次访问20.0.0.26