apache页面优化(压缩gzip/deflate、网页缓存expires、隐藏版本信息default、防盗链rewrite)

在企业中,部署Apache后只采用默认的配置参数,会引发网站很多问题,换言之默认配置是针对以前较低的服务器配置的,以前的配置已经不适用当今互联网时代

为了适应企业需求,就需要考虑如何提升Apache的性能与稳定性,这就是Apache优化的内容Ap

一、Apache网页优化

1.1 优化内容

1、配置网页压缩功能
2、配置网页缓存
3、工作模式的选择与参数优化
4、配置隐藏版本号
5、配置防盗链

二、 Apache的压缩模块

2.2 gzip介绍

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

降低了网络传输的字节数,加快网页加载的速度节省流量,改善用户的浏览体验
gzip与搜索引擎的抓取工具有着更好的关系

2.2.1 Apache实现网页压缩的功能模块包括

mod_gzip模块(压缩)
mod_deflate模块(缩小)

2.2.1.1 Apache 1.x

没有内建网页压缩技术,但可使用第三方mod_gzip模块执行压缩

2.2.1.2 Apache 2.x

在开发的时候,内建了mod_deflate这个模块,取代mod_gzip

2.2.2 mod_gzip模块与mod_deflate模块

两者均使用gzip压缩算法,运作原理类似
mod_deflate压缩速度略快,而mod_gzip的压缩比略高
mod_gzip对服务器CPU的占用要高一些
高流量的服务器,使用mod_deflate可能会比mod-gzip加载速度更快

2.3 配置网页压缩功能

2.3.1 启用 网页压缩功能步骤

查看是否安装mod_deflate模块----->修改配置文件启用压缩功能----->访问测试

2.3.2 检查是否已安装mod_deflate模块

执行apachectl -t -D DUMP_MODULES命令

在这里插入图片描述

如果输出中没有deflate_module (static),说明编译时没有安装mod_deflate模块

若没有安装,则要重新编译安装

./configure --enable-deflate...
make && make install

2.4 网页压缩

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

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 \
--enable-charset-lite \
--enable-cgi \
--enable-deflate

make && make install
 
 
--enable-deflate          加入mod deflate模块

在这里插入图片描述

3、配置mod_deflate模块启用

vim /usr/local/httpd/conf/httpd.conf
--52行-修改
Listen 192.168.238.20:80
--105行--取消注释
LoadModule deflate_module modules/mod_deflate. so     开启mod deflate模块
--197行--取消注释,修改 
ServerName www.xyw.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
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

先将game.jpg文件传到/usr/local/httpd/htdocs目录下
在这里插入图片描述

vim index.html

<html><body><h1> It works ! It works! It works!It works ! It works! It works !It works ! It works! It works ! It works ! It works! Itworks! It works! It works!It works! It works!It works ! It works!It works! It works!It works! It works !It works ! It works! Itworks! It works! It works!It works! It works!It works! It works!It works! It works!It works! It works!It works ! It works!Itworks! It works! It works!It works! It works!It works! It works!It works! It works!It works! It works!It works ! It works! Itworks!It works! It works!It works! It works!It works! It works !It works! It works !It works! It works !It works! It works! Itworks!</h1>
<img src="gou.jpg"/>
</body></html>

在这里插入图片描述
在这里插入图片描述

方法一:

在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络---->选择 HTML、ws、其他
访问http://192.168.238.20,双击200响应消息查看响应头中包含Content-Encoding:gzip

在这里插入图片描述

方法二:

在Windows系统中依次安装Microsoft.NET4和fiddler软件,打开fiddler软件
选择inspectors---->选择Headers
浏览器访问http://192.168.238.20,双击200响应消息查看Content-Encoding: gzip

在这里插入图片描述

三、配置网页的缓存时间

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

3.1 启用网页缓存功能步骤

查看是否安装mod_expire模块---->修改配置文件启用缓存功能----->访问测试

3.1.1 网页缓存

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-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--enable-deflate \           加入mod_deflate模块
--enable-expires             加入mod_expires模块
make && make install

在这里插入图片描述

3、配置mod_expires模块启用

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

--52行--修改
Listen 192.198.238.20:80
--111行--取消注释
LoadModule expires_module modules/mod_expires.so        开启mod expires模块
--199行--取消注释,修改
ServerName www.xyw.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_expires模块是否已安装
deflate module (shared)                             已安装的正确结果

systemctl start httpd.service

在这里插入图片描述

5、测试缓存是否生效

cat /usr/local/httpd/htdocs/index.html

在这里插入图片描述

方法一:

在Linux系统中,打开火狐浏览器,右击点查看元素
选择 网络---->选择HTML、WS、其他
访问http://192.168.238.20,双击200消息查看响应头中包含 Expires 项

在这里插入图片描述

方法二:

在Windows系统中依次安装Microsoft.NET4和fiddler软件,打开fiddler软件
选择inspectors---->选择Headers
浏览器访问http://192.168.238.20,双击200消息查看Expires项

在这里插入图片描述

四、配置Apache隐藏版本信息

Apache的版本信息,透露了一定的漏洞信息,从而给网站带来安全隐患
生产环境中要配置Apache隐藏版本信息

4.1 隐藏版本信息

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 start httpd.service

在这里插入图片描述

在这里插入图片描述

浏览器访问

http://192.168.80.10,双击200消息查看Server项

在这里插入图片描述
在这里插入图片描述

五、配置Apache实现防盗链

防盗链是防止别人的网站代码里面盗用我们自己服务器上的图片、文件、视频等相关资源
如果别人盗用网站的这些静态资源,明显的是会增大服务器的带宽压力
作为网站的维护人员,要杜绝服务器的静态资源被其他网站盗用

5.1 Apache防盗链

5.1.1 检查是否安装mod rewrite模块

apachectl -t -D DUMP_MODULES | grep "rewrite"

5.1.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

5.1.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://xyw.com/.*$ [NC]          设置匹配规则
RewriteCond %{
    
    HTTP_REFERER} !^http://xyw.com$ [NC]
RewriteCond %{
    
    HTTP_REFERER} !^http://www.xyw.com/.*$ [NC]
RewriteCond %{
    
    HTTP_REFERER} !^http://www.xyw.com/$ [NC]
RewriteRule .*\.(gif|jpg|swf)$ http://www.xyw.com/error.png   设置跳转动作
</Directory>

在这里插入图片描述
在这里插入图片描述

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":表示转发到这个路径。
整个配置的含义是 使用本网站以外的网站域名 访问本站的图片文件时,显示error.png这个图片。

5.1.4 网页准备(可以反过来先配盗链主机即可检测是否盗链成功)

Web源主机配置

cd /usr/local/httpd/htdocs
将gou.jpg、error.png文件传到/usr/local/httpd/htdocs目录下
vim index.html

<html><body><h1>this is xyw.com!</h1>
<img src="gou.jpg"/>
</body></html>

echo "192.168.238.20 www.xyw.com" >> /etc/hosts

在这里插入图片描述
在这里插入图片描述
盗链主机

yum install -y httpd

vim /var/www/html/index.html     yum安装的httpd服务的默认路径为/var/www/html/
<html><body><h1>IT WORKS!</h1>
<img src="http://192.168.200.50/gou.jpg"/>
</body></html>

echo "192.168.238.20 www.xyw.com" >> /etc/hosts
echo "192.168.238.10 www.benet.com" >> /etc/hosts

systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

5.1.5在盗图网站主机上进行浏览器验证

http://www.benet.com

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IvyXYW/article/details/112312665