apache下用expires_module让浏览器缓存静态文件

本文详细介绍了如何通过Apache配置文件来开启并设置静态资源的缓存,包括CSS、JS、图片及HTML文件,以减轻服务器压力并提高网站加载速度。通过配置ExpiresActive和ExpiresByType指令,可以实现对不同类型的静态文件设置合理的缓存时间,如CSS文件缓存83天,JS文件同样缓存83天,HTML、图片文件也分别设置为83天的缓存期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

让浏览器缓存CSS、JS、图片、静态文件等是很重要的事情,这样可以减轻服务器的压力,省的浏览器经常要去服务端下载这些静态文件。下面看看配置方法吧。
1.开启apache扩展模块mod_expires.so,在apache的配置文件中加入下面一行代码。
LoadModule expires_module modules/mod_expires.so
2.配置缓存,在apache配置文件中加入下面一段代码
<IfModule expires_module>
    #打开缓存
    ExpiresActive on 
    #css文件缓存7200000/3600/24=83天
    ExpiresByType text/css A7200000
    #js文件缓存7200000/3600/24=83天
    ExpiresByType application/x-javascript A7200000
    ExpiresByType application/javascript A7200000
    #html文件缓存7200000/3600/24=83天
    ExpiresByType text/html A7200000
    #图片文件缓存7200000/3600/24=83天
    ExpiresByType image/jpeg A7200000
    ExpiresByType image/gif A7200000
    ExpiresByType image/png A7200000
    ExpiresByType image/x-icon A7200000
</IfModule>
3.测试
访问有图片,css,js,html的文件
http://localhost:8080/test/test.html
用firebug可以看到响应头信息包含
Expires Tue, 13 Sep 2011 15:41:09 GMT
Cache-Control max-age=7200000

# LAMP Auto Install Script for CentOS 7 #centos7下编写自动化脚本搭建lamp架构,下载目录为/opt/lamp1/,需求版本:apache-2.4.63、php-7.4.33、mysql-8.0.41-1.el9.x86_64.rpm-bundle.tar,php不用单独启动,和apache联动即可 #要求: #安装apache和php的依赖模块apr-1.7.5、apr-util-1.6.3、ibiconv-1.15、libmcrypt-2.5.8、mhash-0.9.9.9、mcrypt-2.6.8、libmcrypt-devel并做好环境变量和软连接 #apache开启模块如下模块并解决依赖问题{ core_module (static) so_module (static) http_module (static) mpm_prefork_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) socache_shmcb_module (shared) socache_dbm_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) expires_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) ssl_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared) rewrite_module (shared) php5_module (shared) proxy_module (shared) proxy_ajp_module (shared) proxy_balancer_module (shared) proxy_connect_module (shared) proxy_http_module (shared) slotmem_shm_module (shared)} #php开启模块如下模块并解决依赖问题{bcmath bz2 Core ctype curl date dom ereg fileinfo filter gd gettext hash iconv json libxml mbstring mcrypt mhash mysql mysqli mysqlnd openssl pcntl pcre PDO pdo_mysql pdo_sqlite Phar posix Reflection session shmop SimpleXML soap sockets SPL sqlite3 standard sysvmsg sysvsem sysvshm tokenizer xml xmlreader xmlwriter zip zlib} #验证apache和php联动访问,MySQL登录和初始化、密码设置为1hblsqt2zhlmcl#@!
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值