在Apache2.4.7(ubuntu)上设置服务器缓存

在Apache2.4.7(ubuntu)上设置服务器缓存,ubuntu,caching,apache2.4,Ubuntu,Caching,Apache2.4,在Apache2.4上设置服务器缓存的最佳方法是什么 我搜索了互联网,找到了一些Apache2.2特有的资源和教程,但是Apache2.2之后,Apache2.2就放弃了模块,并重新命名了其他模块。具体来说,我想在我的Ubuntu环境中使用Apache2.2“mod_cache”、“mod_disk_cache”和“mod_mem_cache”的等价物设置缓存 例如,现有文档指的是启用: mod_缓存、mod_磁盘缓存和mod_内存缓存 在my/etc/apache2/mods可用目录中,似乎相

在Apache2.4上设置服务器缓存的最佳方法是什么

我搜索了互联网,找到了一些Apache2.2特有的资源和教程,但是Apache2.2之后,Apache2.2就放弃了模块,并重新命名了其他模块。具体来说,我想在我的Ubuntu环境中使用Apache2.2“mod_cache”、“mod_disk_cache”和“mod_mem_cache”的等价物设置缓存

例如,现有文档指的是启用: mod_缓存、mod_磁盘缓存和mod_内存缓存

在my/etc/apache2/mods可用目录中,似乎相关的mods有: cache和cache_disk(注意不同的命名约定),甚至没有类似于mem_cache的东西(还有其他对socache等的引用,但它们是不同的)

这里有一个很好的资源帮助我走到了这一步()。apache文档很好地解释了模块的功能,但没有解释如何设置模块

更新: 我正在运行Ubuntu 14.01.1 LTS 确认apache在2.4中删除了mem_缓存-但仍在寻找任何最新的缓存资源/教程

更新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实用程序并配置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上第二次点击return,时间戳不应该改变,但事实并非如此,因此我认为我错过了一些东西/没有完成此操作。

我遇到了同样的问题,但最终找到了一个帮助教程:

以上文章几乎完美无瑕,这篇文章也可能有帮助:

但您仍然可能会面临诸如“缓存未命中、尝试选择实体保存”或根本没有缓存之类的问题

首先,如果内容长度未知,缓存似乎不起作用。 因此,如果缓存不适用于脚本,请打开缓存头并首先检查所有头。 否则,您可能会始终得到X-Cache-Detail:“缓存未命中:正在尝试实体保存”

确定缓存是否工作的最简单方法可能是:

curl -s -D - http://xxx.yyy.com/yorsite | head -n 15
然后,您将看到缓存命中是否发生在第二次尝试和发送所有其他头之后。将-n参数调整为在特定情况下标题的行数

当然,为mod_缓存设置日志可能会有所帮助

LogFormat "%h %l %u %t \"%r\" %{cache-status}e %>s %b" cache
CustomLog ${APACHE_LOG_DIR}/mod_cache.log cache
设置正确的会话\u缓存\u限制器很重要(对于“”或“公共”、“必须重新验证”,请尝试…)

总结cachig(针对Apache 2.4.7)的常见问题如下:

  • VirtualHost节中缺少CacheRoot
  • 缺少CacheQuickHandler off的设置(如果使用mod_重写映射来重写URL,则可能无法工作,或者缓存根本无法工作)
  • 未知或缺少内容长度头(请注意,简单的PHP脚本将发送它,更复杂的可能不会)
  • 缺少会话缓存限制器(“”)或设置为“专用”-导致在缓存控制标头中发送“无存储”、“无缓存”
因此,对于动态内容(php等)的缓存,您可能需要执行以下操作:

ob_start();
...your code...
$PageContent = ob_get_contents();
ob_end_clean();

// Set header for content length
header('Content-Length: ' . strlen($PageContent));

// Send the full content
print $PageContent;
然后,您可以享受性能提升:)

最终工作配置:

<VirtualHost *:80>
        ServerName devel.artikul.cz
        DocumentRoot /var/www/xxx.com

        ErrorLog ${APACHE_LOG_DIR}/xxx.com.log
        CustomLog ${APACHE_LOG_DIR}/xxx.com.access.log combined

        LogFormat "%h %l %u %t \"%r\" %{cache-status}e %>s %b" cache
        CustomLog ${APACHE_LOG_DIR}/mod_cache.log cache

        CacheQuickHandler off
        CacheRoot /var/cache/apache2/mod_cache_disk

        CacheIgnoreHeaders Set-Cookie
        CacheLock on
        CacheLockPath /tmp/mod_cache-lock
        CacheLockMaxAge 5
        CacheDetailHeader on

        #example disk caching
        CacheEnable disk "/xxxx.php"
        CacheEnable disk "/blog"
        CacheEnable disk "/content"
        CacheHeader on

        CacheDefaultExpire 600
        CacheMaxExpire 86400
        CacheLastModifiedFactor 0.5
        CacheMaxFileSize 1000000
        CacheMinFileSize 1

        ExpiresActive on
        ExpiresDefault "access plus 5 minutes"

        Header merge Cache-Control public
        FileETag All

</VirtualHost>

ServerName devel.artikul.cz
DocumentRoot/var/www/xxx.com
ErrorLog${APACHE_LOG_DIR}/xxx.com.LOG
CustomLog${APACHE_LOG_DIR}/xxx.com.access.LOG组合
日志格式“%h%l%u%t\%r\”%{cache status}e%>s%b”缓存
CustomLog${APACHE\u LOG\u DIR}/mod\u cache.LOG cache
快速处理程序关闭
CacheRoot/var/cache/apache2/mod\u cache\u磁盘
CacheIgnoreHeaders集Cookie
锁定
CacheLockPath/tmp/mod_cache-lock
CacheLockMaxAge 5
上的CacheDetailHeader
#磁盘缓存示例
缓存启用磁盘“/xxxx.php”
缓存启用磁盘“/博客”
缓存启用磁盘“/内容”
上的CacheHeader
CacheDefaultExpire600
CacheMax86400
CacheLastModifiedFactor 0.5
CacheMaxFileSize 1000000
CacheMinFileSize 1
过期于
ExpiresDefault“访问加5分钟”
头合并缓存控制公共
FileTag全部

如果控件长度未知,请不要忘记缓存不起作用。注意: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
<VirtualHost *:80>
        ServerName devel.artikul.cz
        DocumentRoot /var/www/xxx.com

        ErrorLog ${APACHE_LOG_DIR}/xxx.com.log
        CustomLog ${APACHE_LOG_DIR}/xxx.com.access.log combined

        LogFormat "%h %l %u %t \"%r\" %{cache-status}e %>s %b" cache
        CustomLog ${APACHE_LOG_DIR}/mod_cache.log cache

        CacheQuickHandler off
        CacheRoot /var/cache/apache2/mod_cache_disk

        CacheIgnoreHeaders Set-Cookie
        CacheLock on
        CacheLockPath /tmp/mod_cache-lock
        CacheLockMaxAge 5
        CacheDetailHeader on

        #example disk caching
        CacheEnable disk "/xxxx.php"
        CacheEnable disk "/blog"
        CacheEnable disk "/content"
        CacheHeader on

        CacheDefaultExpire 600
        CacheMaxExpire 86400
        CacheLastModifiedFactor 0.5
        CacheMaxFileSize 1000000
        CacheMinFileSize 1

        ExpiresActive on
        ExpiresDefault "access plus 5 minutes"

        Header merge Cache-Control public
        FileETag All

</VirtualHost>