在CentOS系统中,Apache2的缓存策略可以通过配置mod_cache和mod_cache_disk模块来实现。以下是配置缓存策略的步骤:
sudo yum install mod_cache mod_cache_disk
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl restart httpd
/etc/httpd/conf/httpd.conf
),在<IfModule mod_cache.c>
和<IfModule mod_cache_disk.c>
之间添加以下内容:<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/httpd"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
</IfModule>
</IfModule>
这里的配置表示启用磁盘缓存,并将缓存根目录设置为/var/cache/httpd
。CacheDirLevels
和CacheDirLength
设置缓存目录的结构,CacheDefaultExpire
设置默认的缓存过期时间(以秒为单位)。
# 为特定文件类型设置缓存时间
<FilesMatch "\.(jpg|jpeg|png|gif|ico)$">
CacheEnable disk /
CacheDefaultExpire 86400
</FilesMatch>
# 仅为特定的URL路径启用缓存
<Location "/static/">
CacheEnable disk /
CacheDefaultExpire 86400
</Location>
sudo systemctl restart httpd
现在,Apache2将根据配置的缓存策略缓存文件。可以使用浏览器的开发者工具或类似工具检查缓存是否正常工作。