【发布时间】:2015-04-27 09:16:48
【问题描述】:
在 Apache 2.4 上设置服务器缓存的最佳方法是什么?
我搜索了互联网并找到了一些特定于 Apache 2.2 的资源和教程 - 但 Apache 自 2.2 以来已删除模块并重命名了其他模块。具体来说,我想在我的 Ubuntu 环境中使用等效的 Apache 2.2“mod_cache”、“mod_disk_cache”和“mod_mem_cache”来设置缓存。
例如,现有文档指的是启用: mod_cache、mod_disk_cache 和 mod_mem_cache
在我的 /etc/apache2/mods-available 目录中,其中似乎相关的模块是: cache 和 cache_disk (注意不同的命名约定),也没有什么类似于 mem_cache (还有其他对 socache 等的引用,但它们是不同的)
这是一个很好的资源,它帮助我走到了这一步 here (and another)。 apache 文档很好地解释了模块的作用,但没有解释如何设置它们。
更新: 我正在运行 Ubuntu 14.01.1 LTS 发现 apache 在 2.4 中删除了 mem_cache 的确认 - 虽然仍在寻找任何最新的缓存资源/教程
更新2: 似乎实际上没有专门用于在 apache 2.4 上配置缓存的资源,所以这是我迄今为止采取的步骤,可能有助于未来的搜索:
#Check which modules are available
ls /etc/apache2/mods-available
#Enable cache modules
sudo a2enmod cache
sudo a2enmod cache_disk
#restart apache
sudo service apache2 restart
#edit the cache_disk.conf file
sudo vim /etc/apache2/mods-available/cache_disk.conf
取消注释 CacheEnable /disk 行,使 conf 文件看起来像:
<IfModule mod_cache_disk.c>
# cache cleaning is done by htcacheclean, which can be configured in
# /etc/default/apache2
#
# For further information, see the comments in that file,
# /usr/share/doc/apache2/README.Debian, and the htcacheclean(8)
# man page.
# This path must be the same as the one in /etc/default/apache2
CacheRoot /var/cache/apache2/mod_cache_disk
# This will also cache local documents. It usually makes more sense to
# put this into the configuration for just one virtual host.
CacheEnable disk /
# The result of CacheDirLevels * CacheDirLength must not be higher than
# 20. Moreover, pay attention on file system limits. Some file systems
# do not support more than a certain number of inodes and
# subdirectories (e.g. 32000 for ext3)
CacheDirLevels 2
CacheDirLength 1
</IfModule>
重新启动 apache 以使更改生效
sudo service apache2 restart
接下来,我们需要确保使用 apache 实用程序并配置 htcacheclean 来清理缓存何时填满:
aptitude install apache2-utils
#cleans the cache every 30 min and makes sure it doesnt get bigger than 100M
htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i
#configure htclean to start every time the server restarts
sudo vim /etc/rc.local
#add the following lines before the exit 0 line
[...]
/usr/sbin/htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i
[...]
...这就是我目前所能做到的。
更新 3: 我尝试使用新文件 test.php 在浏览器中进行测试:
<?php
header("Cache-Control: must-revalidate, max-age=300");
header("Vary: Accept-Encoding");
echo time()."<br>";
?>
我应该能够在 url 上第二次点击返回,并且时间戳不应该改变,但事实并非如此,所以我认为我错过了一些东西/没有完成这个。
【问题讨论】:
-
注意:htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i 应该是 htcacheclean -d30 -n -t -p /var/cache/apache2/ mod_cache_disk -l 100M -i