【问题标题】:How do I add Cache Control expires header to images?如何将缓存控制过期标头添加到图像?
【发布时间】:2011-10-08 08:53:35
【问题描述】:

我正在尝试加快我网站上的速度。 YSlow 提醒我我的图像缺少过期标题。但是我如何在图像上应用这样的标题呢?

我的应用程序基于 zend 框架。图像和图像一样存储在一个文件夹中,我怎样才能为它们设置过期标头?

【问题讨论】:

    标签: php zend-framework cache-control zend-cache


    【解决方案1】:

    如果您使用的是 apache,在 httpd.conf 中您可以执行以下操作:

    LoadModule expires_module modules/mod_expires.so
    ExpiresActive On
    ExpiresDefault "access plus 300 seconds"
    <Directory "/myProject/webResources">
        Options FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
        ExpiresByType image/gif "access plus 1 day"
        ExpiresByType image/jpg "access plus 1 day"
        ExpiresByType image/png "access plus 1 day"
        ExpiresByType application/x-shockwave-flash "access plus 1 day"
    </Directory>
    

    【讨论】:

      【解决方案2】:

      我昨天遇到了同样的问题...

      1. 确保在生成图像的操作中设置了正确的标题。
      2. 您必须将带有“Content-Type”的 memorize_headers 添加到 frontendOptions 中,并且为了提高性能,还必须添加“Cache-Control”以及您想要设置的任何标头...

      所以来自http://framework.zend.com/manual/en/zend.cache.frontends.html 的 Zend_Cache_Frontend_Page 的示例看起来像这样:

      $frontendOptions = array(
         'lifetime' => 7200,
         'debug_header' => true, // for debugging
         'regexps' => array(
             // cache the whole IndexController
             '^/$' => array('cache' => true),
      
             // cache the whole IndexController
             '^/index/' => array('cache' => true),
      
             // we don't cache the ArticleController...
             '^/article/' => array('cache' => false),
      
             // ... but we cache the "view" action of this ArticleController
             '^/article/view/' => array(
                 'cache' => true,
      
                 // and we cache even there are some variables in $_POST
                 'cache_with_post_variables' => true,
      
                 // but the cache will be dependent on the $_POST array
                 'make_id_with_post_variables' => true
             )
         ),
          'memorize_headers' => array(
              'Content-Type',
              'Cache-Control',
              'Expires',
              'Pragma',
         )
      );
      
      $backendOptions = array(
          'cache_dir' => '/tmp/'
      );
      
      // getting a Zend_Cache_Frontend_Page object
      $cache = Zend_Cache::factory('Page',
                                   'File',
                                   $frontendOptions,
                                   $backendOptions);
      
      $cache->start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-26
        • 2011-10-28
        • 2014-04-11
        • 2015-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多