LAMP架构应用实战—Apache服务mod_expires模块介绍
2020-12-20 03:36
标签:alt png 源文件 计算 否则 require nod root expires 此模块是允许通过Apache配置文件控制HTTP的“expires”和“cache-control”头的内容,用于控制服务器应答时的“expires”头的内容和“cache-control”头的max-age的指令,这个有效期可以设置为对于源文件最后的修改时间或客户端访问的时间 直接编译安装查看如下 chmod 755 /application/apache2.4.23/modules/mod_expires.so [root@Centos ~]# curl -I http://blog.abc.com:9999/ 模块配置有两种模式:主配置里面配置与单个虚拟主机配置文件里配置 上传一张图片到站点目录 本地客户端浏览器访问 [root@Centos blog]# curl -I http://blog.abc.com:9999 [root@Centos blog]# date #port bash name 检查语法并重启服务 查看进程与端口 上传一张图片到站点目录 本地客户端浏览器访问 查看缓存信息 [root@Centos extra]# curl -I https://www.baidu.com/img/bd_logo1.png 四:mod_expires模块的优点 注:但是这个缓存也会有失效的时候就是用户主动清空浏览器缓存或者有效期过期 LAMP架构应用实战—Apache服务mod_expires模块介绍 标签:alt png 源文件 计算 否则 require nod root expires 原文地址:https://blog.51cto.com/mingongge/2555693LAMP架构应用实战—Apache服务
mod_expires缓存模块介绍
一:mod_expires模块介绍
这些HTTP头向客户端表明了内容有效性与持久性,如果客户端本地有缓存,则用户再次访问时读取的内容就是从缓存里读取的(缓存没有失效的情况下),而不是从服务器端读取到的,客户端还会检查缓存中的内容,看看是不是需要从服务器端进行更新,从而增加用户的体验度,减少服务器的压力,实际生产环境中也是调优参数之一二:检查与安装模块
[root@Centos modules]# /application/apache/bin/apachectl -l|grep mod_expi
mod_expires.c
以DSO方式编译安装查看如下
[root@Centos ~]# ll /application/apache/modules/|grep expir
-rwxr-xr-x. 1 root root 37799 Sep 12 03:30 mod_expires.so
但是以上两种方式不能同时安装,否则会出现错误查看帮助
[root@Centos httpd-2.4.23]# ./configure --help |grep expir
--enable-expires Expires header control
具体编译命令如下
[root@Centos httpd-2.4.23]# ./configure\
--enable-expires
以DSO的方式编译安装如下
cd /Downloads/tools/httpd2.2.24/modules/metadata/
/application/apache/bin/apxs -c -i -a mod_expires.c
参数说明
-a 此选项会自动增加一个LoadModule行到httpd.conf文件中,来激活模块,如果此行已存在,则启用
-c 此选项表示需要执行编译操作
-i 此选项表示需要执行安装操作,以安装一个或多个动态共享对象到服务器modules目录中
[root@Centos modules]# cd /Downloads/tools/httpd-2.4.23/modules/metadata/
[root@Centos metadata]# /application/apache/bin/apxs -a -c -i mod_expires.c
处程省略
[activating module `expires‘ in /application/apache2.4.23/conf/httpd.conf]三:配置mod_expires模块
1、查看当前的 http headers信息
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 03:40:44 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT
ETag: "1f-53c122a061992"
Accept-Ranges: bytes
Content-Length: 31
Content-Type: text/html2、配置模块
配置HTTP主配置文件如下
[root@Centos conf]# ls
extra httpd.conf httpd.conf.bak index.html magic mime.types original
[root@Centos conf]# cp httpd.conf httpd.conf.2016-09-09
[root@Centos conf]# vi httpd.conf
前面的内容省略
Options FollowSymLinks
AllowOverride None
Require all granted
Options FollowSymLinks
AllowOverride None
Require all granted
ExpiresActive on
ExpiresDefault "access plus 12 month"
ExpiresByType text/css "access plus 12 month"
ExpiresByType image/gif "access plus 12 month"
ExpiresByType image/jpge "access plus 12 month"
ExpiresByType image/jpg "access plus 12 month"
ExpiresByType image/png "access plus 12 month"
ExpiresByType application/x-shockwave-flash "access plus 12 month"
ExpiresByType application/x-javascript "access plus 12 month"
ExpiresByType video/x-flv "access plus 12 month"
"httpd.conf" 513L, 18662C written
检查语法并重启服务
[root@Centos conf]# /application/apache/bin/apachectl -t
Syntax OK
[root@Centos conf]# /application/apache/bin/apachectl graceful
检查进程与端口
[root@Centos conf]# ps -ef|grep http
root 1717 1 0 09:45 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5319 1717 0 12:13 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5320 1717 0 12:13 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5321 1717 0 12:13 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
root 5404 5022 0 12:13 pts/0 00:00:00 grep http
[root@Centos conf]# lsof -i tcp:9999
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1717 root 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5319 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5320 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5321 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
[root@Centos conf]# lsof -i tcp:8888
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1717 root 6u IPv6 14444 0t0 TCP :ddi-tcp-1 (LISTEN)
httpd 5319 daemon 6u IPv6 14444 0t0 TCP :ddi-tcp-1 (LISTEN)
httpd 5320 daemon 6u IPv6 14444 0t0 TCP :ddi-tcp-1 (LISTEN)
httpd 5321 daemon 6u IPv6 14444 0t0 TCP :ddi-tcp-1 (LISTEN)
[root@Centos blog]# rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring 20160918093614.jpg...
100% 49 KB 49 KB/s 00:00:01 0 Errors
[root@Centos blog]# ll
total 56
-rw-r--r--. 1 root root 50657 Sep 18 09:36 20160918093614.jpg
-rw-r--r--. 1 root root 31 Sep 9 20:19 index.html
http://blog.abc.com:9999/20160918093614.jpg
可以正常显示上传的图片信息
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 04:21:24 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT
ETag: "1f-53c122a061992"
Accept-Ranges: bytes
Content-Length: 31
Cache-Control: max-age=31104000
Expires: Wed, 13 Sep 2017 04:21:24 GMT
#发现没有,这就是缓存的时间期限
Content-Type: text/html
Sun Sep 18 12:21:36 CST 2016说明如果配置HTTP主配置文件,则全局生效,虚拟主机配置文件则不需要配置
配置单个虚拟主机文件如下
ServerAdmin admini@abc.com
DocumentRoot "/data/www/blog"
ServerName blog.abc.com
ServerAlias blog1.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
ExpiresActive on
ExpiresDefault "access plus 12 month"
ExpiresByType text/css "access plus 12 month"
ExpiresByType image/gif "access plus 12 month"
ExpiresByType image/jpge "access plus 12 month"
ExpiresByType image/jpg "access plus 12 month"
ExpiresByType image/png "access plus 12 month"
ExpiresByType application/x-shockwave-flash "access plus 12 month"
ExpiresByType application/x-javascript "access plus 12 month"
ExpiresByType video/x-flv "access plus 12 month"
"httpd-vhosts.conf" 50L, 1821C written
[root@Centos extra]# /application/apache/bin/apachectl -t
Syntax OK
[root@Centos extra]# /application/apache/bin/apachectl graceful
[root@Centos extra]# ps -ef |grep http
root 1717 1 0 09:45 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5088 1717 0 11:52 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5089 1717 0 11:52 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
daemon 5090 1717 0 11:52 ? 00:00:00 /application/apache2.4.23/bin/httpd -k start
root 5176 5022 0 11:53 pts/0 00:00:00 grep http
[root@Centos extra]# lsof -i tcp:9999
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1717 root 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5088 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5089 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
httpd 5090 daemon 8u IPv6 14448 0t0 TCP :distinct (LISTEN)
[root@Centos blog]# rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring 20160918093614.jpg...
100% 49 KB 49 KB/s 00:00:01 0 Errors
[root@Centos blog]# ll
total 56
-rw-r--r--. 1 root root 50657 Sep 18 09:36 20160918093614.jpg
-rw-r--r--. 1 root root 31 Sep 9 20:19 index.html
http://blog.abc.com:9999/20160918093614.jpg
可以正常显示上传的图片信息
[root@Centos blog]# curl -I http://blog.abc.com:9999
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 04:01:36 GMT
Server: Apache/2.4.23 (Unix)
Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT
ETag: "1f-53c122a061992"
Accept-Ranges: bytes
Content-Length: 31
Cache-Control: max-age=31104000
Expires: Wed, 13 Sep 2017 04:01:36 GMT
Content-Type: text/html
[root@Centos blog]# date
Sun Sep 18 12:02:20 CST 2016
初步计算应该是360天的样子,说明配置是正确的
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Sun, 18 Sep 2016 07:07:04 GMT
Content-Type: image/png
Content-Length: 7877
Connection: keep-alive
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Set-Cookie: BAIDUID=01FAEEFF2AF063514442EEC0D5D83A68:FG=1; expires=Mon, 18-Sep-17 07:07:04 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1
Last-Modified: Wed, 03 Sep 2014 10:00:27 GMT
ETag: "1ec5-502264e2ae4c0"
Accept-Ranges: bytes
Cache-Control: max-age=315360000
Expires: Wed, 16 Sep 2026 07:07:04 GMT
百度的LOG图片缓存有效期约为十年,其它的有兴趣可以自己去看
1、提升用户对访问网站的体验度
由于一些文件缓存在本地,访问速度提升了,用户体验也就相应的提升了
2、减少服务器带宽与负载压力
由于用户访问时是读取本地缓存的文件内容,减少了与服务器之间的交互,从而减轻服务器压力
3、节约维护服务器的成本
和上述一样,服务器压力小了,维护人员也会相应的减少,服务器配件配置更新的速度也会相应的慢下来
长按二维码关注微信公众号(友侃有笑)
文章标题:LAMP架构应用实战—Apache服务mod_expires模块介绍
文章链接:http://soscw.com/index.php/essay/37345.html