Apache配置 7.静态元素过期时间

(1)介绍

那到底能缓存多久呢?如果服务器上的某个图片更改了,那么应该访问新的图片才对。这就涉及一个静态文件缓存时长的问题,也叫作“缓存过期时间”。在httpd的配置文件中,我们是可以控制这个时间的。

2)配置

在httpd上我们可以控制文件过期时间,配置如下:

#vim /usr/local/apache2 .4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.111.com"

    ServerName www.111.com

    ServerAlias 111.com

    <IfModule mod_expires.c>

        ExpiresActive on

        ExpiresByType image/gif "access plus 1 days“

        ExpiresByType image/jpeg "access plus 24 hours"

        ExpiresByType image/png "access plus 24 hours'

        ExpiresByType text/css "now plus 2 hour"

        ExpiresByType application/x-javascript "now plus 2 hours"

        ExpiresByType application/javascript "now plus 2 hours"

        ExpiresByType application/x-shockwave-flash "now plus 2 hours"

        ExpiresDefault "now plus 0 min"

  </IfModule>

    ErrorLog "logs/111.com-error_log"

    CustomLog "logs/111.com-access_log" combined

</VirtualHost>

   //这里gif,jpeg, png格式的文件过期时长为1天,css. js. flash格式的文件过期时长为2小时

#/usr/local/apache2.4/bin/apachectl -t

Syntax OK

 

 

 

 

 

#/usr/local/apache2.4/bin/apachectl graceful

(3)配置验证

检查httpd是否加载expires模块:

#/usr/local/apache2.4/bin/apachectl -M |grep -i expires

   //没有输出则说明当前httpd并不支持expires模块,所以需要修改配置文件

#vim /usr/local/apache2.4/conf/httpd.conf

   //搜索关键词expires,找到这一行

#LoadModule expires_ module modules/mod_ expires .so

   //把前面#删掉

 

 

#/usr/local/apache2.4/bin/apachectl graceful

   //重新加载服务

#/usr/local/apache2.4/bin/apachectl -M|grep -i expires

expires_module(share)

 

   //有这行输出说明正确加载expires模块

 

 

下面进行测试:

上传linux.jpeg文件

# curl -xlocalhost:80 www.111.com/image/linux.jpeg -I

   //max-age=86400说明将缓存86400秒,max-age=0说明没有该类型缓存文件

 

测试

 

 

posted @ 2021-03-09 10:11  清空。  阅读(129)  评论(0)    收藏  举报