Linux—Apache网页优化—网页缓存
一.配置网页的缓存时间:
1.通过mod_ expire模块配置Apache ,使网页能在客户端浏览器缓存一段时间,以避免重复请求
2.启用mod_ expire模块后,会自动生成页面头部信息中的Expires标签和Cache-Control标签,从而降低客户端的访问频率和次数,达到减少不必要的流量和增加访问速度的目的
3.启动网页缓存功能步骤
1)查看是否安装mod_expire模块
2)修改配置文件启用缓存功能
3)抓包测试
4.查看是否安装了mod_ expire模块
1)./usr/local/apache/bin/apachectl -t -D DUMP_ MODULES
2).如果输出中没有expires_ module (static) ,则说明编译时没有安装mod_ expires
5.如果没有安装则要重新编译安装
1)./configure - -enable- expires…
2)make && make install
6.修改httpd.conf配置文件
1)启用mod_ expires模块 ,并设置http协议下任意格式的文档均60秒后过期
7.重启httpd服务
二.网页缓存
1.准备压缩包:
2.解压缩包:
[root@localhost mnt]# tar zxvf apr-1.4.6.tar.gz -C /opt
[root@localhost mnt]# tar zxvf apr-util-1.4.1.tar.gz -C /opt
[root@localhost mnt]# tar zxvf httpd-2.4.2.tar.bz2 -C /opt
[root@localhost mnt]# cd /opt
[root@localhost opt]# mv apr-1.4.6 httpd-2.4.2/srclib/apr
[root@localhost opt]# mv apr-util-1.4.1 httpd-2.4.2/srclib/apr-util
3.安装环境包:
[root@localhost opt]# yum install gcc gcc-c++ pcre-devel pcre zlib-devel make -y
4.配置:
[root@localhost opt]# cd httpd-2.4.2/
[root@localhost httpd-2.4.2]# ./configure \
--prefix=/usr/local/httpd \
--enable-deflate \
--enable-expires \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
5.安装编译
[root@localhost httpd-2.4.2]# make && make install
6.创建软连接,修改配置文件:
[root@localhost httpd-2.4.2]# ln -s /usr/local/httpd/conf/httpd.conf /etc/http.conf
[root@localhost httpd-2.4.2]# vim /etc/http.conf
7修改配置文件
监听端口:
服务名:
开启服务:
末行添加:
8.验证语法:
[root@localhost httpd-2.4.2]# /usr/local/httpd/bin/apachectl -t
Syntax OK
9.开服务看端口:
[root@localhost httpd-2.4.2]# /usr/local/httpd/bin/apachectl start
[root@localhost httpd-2.4.2]# netstat -ntap | grep 80
tcp 0 0 192.168.88.131:80 0.0.0.0:* LISTEN 30669/httpd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1280/dnsmasq
10.关闭防火墙
[root@localhost httpd-2.4.2]# systemctl stop firewalld.service
[root@localhost httpd-2.4.2]# setenforce 0
11.验证脚本:
[root@localhost httpd-2.4.2]# cd /usr/local/httpd/bin/
[root@localhost bin]# ./apachectl -t -D DUMP_MODULES | grep "expires"
expires_module (shared)
12.去测试机上查看
13.把配置文件里的50s改成40s,并重启服务:
[root@localhost bin]# vim /etc/http.conf
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 40 seconds"
</IfModule>
[root@localhost bin]# /usr/local/httpd/bin/apachectl stop
[root@localhost bin]# /usr/local/httpd/bin/apachectl start