LAMP架构十二( Apache静态元素过期时间)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sj349781478/article/details/84632949

十二、静态元素过期时间
      对于短时间内的重复访问,将静态元素设置为仅在第一次下载,不仅可以减轻服务器压力,又可以提升用户访问体验。但这个时间不能设置的过长,具体情况根据网站自身内容的更新频率而定,对于用户请求超过了设置的时间,客户端会重新向服务器请求静态元素,以保证页面最新。

配置静态元素过期时间,配置的时候注意空格和拼写:
[root@global ~]# vi /etc/httpd/conf.d/virtual.conf 
虚拟主机增加静态元素规则:
<VirtualHost *:8090>                                                                                 80是http的服务端口,不用改
 ServerAdmin [email protected]                                                        网站管理员邮箱,设置成自己的网站
 DocumentRoot "/var/www/html/a"                                                           虚拟主机根目录放网站程序
    ServerName a.com                                                                                                网站名,域名                  
    ServerAlias www.a.com                                                                     网站别名,域名别名,可写多个要用空格隔开 


    <IfModule mod_rewrite.c>                                                                     需要mod_rewrite模块支持
        RewriteEngine on                                                                                              打开rewrite功能
        RewriteCond %{HTTP_HOST} !^www.a.com$                            将非www.test.com的域名请求跳转至此
        RewriteRule ^/(.*)$ http://www.baidu.com/$1 [R=301,L]                     状态码301永久跳转,L=last,跳一次
                                                   定义跳转规则,将以^/(.*)$开头结尾的域名请求跳转,$1代表^/(.*)$
    </IfModule>


<IfModule mod_expires.c>                                                                 加载mod_expires.c有效期模块
    ExpiresActive on                                                                                             打开有效期功能开关
    ExpiresByType image/gif  "access plus 1 days"                                                             有效期1天
    ExpiresByType image/jpeg "access plus 24 hours"                                                  有效期24小时
    ExpiresByType image/jpg "access plus 24 hours"                                                    有效期24小时
    ExpiresByType image/png "access plus 24 hours"                                                   有效期24小时
    ExpiresByType text/css "now plus 2 hour"                                                                  有效期2小时
    ExpiresByType application/x-javascript "now plus 2 hours"                                        有效期2小时
    ExpiresByType application/javascript "now plus 2 hours"                                           有效期2小时
    ExpiresByType application/x-shockwave-flash "now plus 2 hours"                             有效期2小时
    ExpiresDefault "now plus 0 min"                                                                   有效期0分钟,不缓存
</IfModule>


    ErrorLog "logs/test.com-error_log"                                                                     错误日志保存路径
    SetEnvIf Request_URI ".*\.gif$" img                                             日志记录规则,变量表示.gif文件
    SetEnvIf Request_URI ".*\.jpg$" img                                           日志记录规则,变量表示.jpg文件
    SetEnvIf Request_URI ".*\.png$" img                                         日志记录规则,变量表示.png文件
    SetEnvIf Request_URI ".*\.bmp$" img                                       日志记录规则,变量表示.bmp文件
    SetEnvIf Request_URI ".*\.swf$" img                                          日志记录规则,变量表示.swf文件
    SetEnvIf Request_URI ".*\.js$" img                                                日志记录规则,变量表示.js文件
    SetEnvIf Request_URI ".*\.css$" img                                            日志记录规则,变量表示.cs文件
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/test.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>  

确认有误expires_module模块

测试语法并重新加载配置
[root@global ~]# /usr/sbin/apachectl -t
Syntax OK
[root@global ~]# /usr/sbin/apachectl  graceful
 

测试访问:
[root@global ~]# touch /var/www/html/a/images/test.jpep
 我在日志记录规则里未排除jpeg类型元素的记录,但在有效期规则里设置了jpeg类型元素的有效期

未配置过期时间之前:

配置了过期时间之后:

查看访问记录:

状态码304,表示已缓存在远程客户端,即访问者的电脑,第一次访问的状态码为200,表示获取成功。

猜你喜欢

转载自blog.csdn.net/sj349781478/article/details/84632949