在 Apache 2.4.7 上设置服务器缓存 (ubuntu)

Setting up server cache on Apache 2.4.7 (ubuntu)

在 Apache 2.4 上设置服务器缓存的最佳方法是什么?

我在 Internet 上进行了搜索,找到了一些特定于 Apache 2.2 的资源和教程 - 但 Apache 自 2.2 以来已经删除了模块并重命名了其他模块。具体来说,我想使用 Apache 2.2 "mod_cache"、"mod_disk_cache" 和 "mod_mem_cache" 的等价物在我的 Ubuntu 环境中设置缓存。

例如,现有文档指的是启用: 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 上第二次点击 return 并且时间戳应该不会改变,但事实并非如此,所以我想我错过了什么/没有完成这个。

我遇到了同样的问题,但终于找到了帮助教程: https://www.digitalocean.com/community/tutorials/how-to-configure-apache-content-caching-on-ubuntu-14-04

以上文章几乎是完美的,这篇文章也可能对您有所帮助:http://www.jonasjohn.de/snippets/php/caching.htm

但您仍然可能会遇到 "CACHE MISS, ATTEMMPTING ENTITY SAVE" 或根本没有缓存的问题。

首先,如果 Content-length 未知,缓存似乎不起作用。 因此,如果缓存对您的脚本不起作用,请打开缓存 headers 并首先检查所有 headers。 否则你可能总是 X-Cache-Detail: "cache miss: attempting entity save".

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

curl -s -D - http://xxx.yyy.com/yorsite | head -n 15

然后您会看到在第二次尝试和所有其他 header 发送后是否发生缓存命中。在特定情况下将 -n 参数调整为您的 header 有多少行。

当然,为 mod_cache 设置日志记录可能会有帮助。

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

设置正确的 session_cache_limiter 很重要(对“”或 "public"、"must_revalidate",试试...)

总结 cachig(对于 Apache 2.4.7)的常见问题是:

  • VirtualHost 部分缺少 CacheRoot
  • 缺少 CacheQuickHandler 关闭设置(如果您使用 mod_rewrite 映射到重写的 URL 可能不起作用或缓存根本不起作用)
  • 未知或丢失 Content-Length header(注意,简单的 PHP 脚本会发送它,更复杂的可能不会)
  • 缺少 session_cache_limiter("") 或设置为 "private" - 导致在 Cache-Control header[ 中发送 "no_store"、"no-cache" =42=]

因此,对于动态内容的缓存(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>