Apache网页优化

46 篇文章 3 订阅
2 篇文章 1 订阅

1.Apache网页优化

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

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

在这里插入图片描述

1.1 网页压缩

gzip介绍

在这里插入图片描述
Apache的压缩模块
在这里插入图片描述
在这里插入图片描述

启用网页压缩功能步骤

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

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

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

<IfModule mod_deflate.c>
AddOutputFilterByType  DEFLATE  text/html text/plain text/css text/xml text/javascript text/jpg text/png
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
</IfModule>

在这里插入图片描述

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

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

1.2 网页缓存

在这里插入图片描述

启用网页缓存功能步骤

在这里插入图片描述

在这里插入图片描述
安装expires模块

[root@clr /opt/httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi \
> --enable-deflate \
> --enable-expires

修改httpd.conf配置文件
在这里插入图片描述
在这里插入图片描述

1.2.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 \
--enable-expires			#加入mod_expires 模块

make && make install

(3)配置mod_expires模块启用

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.kgc.com:80
--末行添加--
<IfModule mod_expires.c>
  ExpiresActive On								#打开网页缓存功能
  ExpiresDefault "access plus 60 seconds"		#设置缓存60</IfModule>

在这里插入图片描述

vim /usr/local/httpd/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so

<IfModule mod_expires.c>
  ExpiresActive On                                              
  ExpiresDefault "access plus 60 seconds"
</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.80.20/test/hello.html,=浏览器中右击查看元素,选择网络,查看Expires项==

方法二:
在Windows系统中依次安装Microsoft.NET4 和fiddler软件,打开fiddler软件
选择 inspectors —> 选择 Headers
浏览器访问 http://192.168.80.20/test/hello.html,浏览器中右击查看元素,选择网络,查看Expires项

在这里插入图片描述

2.Apache安全优化

2.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 restart httpd.service

浏览器访问 http://192.168.80.20/test/hello.html ,双击200消息查看Server项

在这里插入图片描述

2.2 防盗链

2.2.1 模拟防盗链

在这里插入图片描述

两台主机配置与功能

(1)两台主机配置测试页面

(2)盗链网站的测试网页,盗用源主机网站目录下的一个logo.jpg文件

(3)在浏览器中访问验证

主机名:IP地址访问域名用途
CentOS 7-2:192.168.80.20http://www.gzy20.com/test/hello.html源主机
CentOS 7-4:192.168.80.40http://www.accp.com/test.html盗链网站

防盗链模拟步骤

(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://gzy20.com/.*$ [NC]				#设置匹配规则
  RewriteCond %{HTTP_REFERER} !^http://gzy20.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.gzy20.com/.*$ [NC]
  RewriteCond %{HTTP_REFERER} !^http://www.gzy20.com/$ [NC]
  RewriteRule .*\.(gif|jpg|swf)$ http://www.gzy20.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 这个图片

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

(4)网页准备

Web源主机CentOS 7-2配置:

cd /usr/local/httpd/htdocs/test
将game.jpg、error.png文件传到/usr/local/httpd/htdocs/test目录下
vim hello.html
<html><body><h1>Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!Hello World mygirl!</h1>
<img src="game.jpg"/>
</body></html>

echo "192.168.80.20 www.gzy20.com" >> /etc/hosts 
echo "192.168.80.40 www.accp.com" >> /etc/hosts 

盗链网站主机CentOS 7-4配置:


cd /usr/local/httpd/htdocs				#yum安装的httpd服务的默认路径为/var/www/html/
cd /var/www/html/
vim test.html
<html><body><h1>his is a daodian web!!!!!</h1>
<img src="http://www.gzy20.com/test/game.jpg"/>
</body></html>

echo "192.168.80.20 www.gzy20.com" >> /etc/hosts 
echo "192.168.80.40 www.accp.com" >> /etc/hosts 

(5)在盗图网站主机上进行浏览器验证

http://www.accp.com/test.html

设置防盗链之前的浏览器访问网页页面;
在这里插入图片描述

设置防盗链之后的浏览器访问网页页面;
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌上花开,静待绽放!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值