实验步骤
配置网页缓存时间的目的
通过mod _expire模块配置Apache,使网页能在客户端浏览器缓存一段时间,以避免重复请求
启用mod_expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,客户端浏览器根据标签决定下次访问是在本地机器的缓存中获取页面,不需要向服务器再次发出请求,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
一、先检查是否安装了mod_expires模块
apachectl -t -D DUMP_MODULES | grep "expires"
二、如果没有安装mod_expires模块,就要重新编译安装Apache添加mod_expires模块
systemctl stop httpd
cd /usr/local/httpd/conf
mv httpd.conf httpd.conf.bak3
cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-deflate --enable-expires
make && make install
三、配置mod_expires模块启用
cd /usr/local/httpd/conf
vim httpd.conf
--52行修改监听地址--
Listen 192.168.73.188:80
--105行取消注释--
LoadModule deflate_module modules/mod_deflate.so
--111行取消注释--
LoadModule expires_module modules/mod_expires.so
--199行修改--
ServerName www.lwh.com:80
--末行添加
<IfModule mod_expires.c>
ExpiresActive On #打开网页缓存功能
ExpiresDefault "access plus 60 seconds" #设置缓存60秒
</IfModule>
systemctl restart httpd
apachectl -t -D DUMP_MODULES | grep "expires"
ss -natp | grep httpd
四、测试mod_expires是否生效
网页中按F12,点击网络,如果有重新载入选项点击,再选择图片,右边点击响应头
隐藏Apache版本信息
vim /usr/local/httpd/conf/httpd.conf
--491行取消注释--
vim /usr/local/httpd/conf/extra/httpd-default.conf
--55行修改
ServerTokens Prod
systemctl restart httpd