转 Apache Mod cache usage and configuration

2019-03-27 01:21|来源: 网路

Apache mod cache

 

Two files will be created, containing cache-data and cache-header

 

To store items in the cache, mod_cache_disk creates a 22 character hash of the URL being requested. This hash incorporates the hostname, protocol, port, path and any CGI arguments to the URL, as well aselements defined by the Vary header to ensure that multiple URLs do not collide with one another. This hash is used as a prefix for the naming of the files specific to that URL within the cache

 

Apache will check cache freshness by comparing current request headers and cached headers

 

 

 

Cache or not cache

  1. Caching must be enabled forthis URL. See the CacheEnable and CacheDisable directives.

  2. The response must have a HTTPstatus code of 200, 203, 300, 301 or 410.

  3. The request must be a HTTP GETrequest.

  4. If the response contains an"Authorization:" header, it must also contain an"s-maxage", "must-revalidate" or "public" optionin the "Cache-Control:" header, or it won't be cached.

  5. If the URL included a querystring (e.g. from a HTML form GET method) it will not be cached unless the response specifies an explicit expiration by including an "Expires:"header or the max-age or s-maxage directive of the "Cache-Control:"header, as per RFC2616 sections 13.9 and 13.2.1.

  6. If the response has a status of200 (OK), the response must also include at least one of the "Etag","Last-Modified" or the "Expires" headers, or the max-age ors-maxage directive of the "Cache-Control:" header, unless the CacheIgnoreNoLastModdirective has been used to require otherwise.

  7. If the response includes the"private" option in a "Cache-Control:" header, it will notbe stored unless the CacheStorePrivate has been used to require otherwise.

  8. Likewise, if the responseincludes the "no-store" option in a "Cache-Control:"header, it will not be stored unless the CacheStoreNoStore has been used.

  9. A response will not be storedif it includes a "Vary:" header containing the match-all"*".

 

See http://httpd.apache.org/docs/trunk/caching.html

 

 

Concurrency issue:

We found that when multi thread isaccessing apache cache, there are lot of 304 response returned. When we test itby single thread, we are not seeing this problem though. This is related tocache lock.  

 

 

 

The cache lock is a mechanism to avoid overwhelming the back end. When cache expires, a lot of requests will hit backend concurrently, which creates load spike. Cache lock is an indicate that cache is being updated and request should not go to backend. The parameterCacheLockMaxAge determines how long the cache should be locked. Its value should be governed by how fast cache reload can be performed.

 

The frequency of seeing 304 is highly dependson the parameter CacheLockMaxAge. The larger value, the more frequent we willsee 304. If we disable cacheLock, we will not see 304 anymore.

 

 

 

Normally we want different cache strategyto be applied to different url

We allow a path string

Eg:  CacheEnable disk /solr/

 

Note that apache cache does not support pattern match in url, so any * in the url is not interpreted correctly. So if we are to disable caching for certain url pattern

SetEnvIf REQUEST_URI admin no-cache

 

 

Debug:

CacheHeader on

CacheDetailHeader on

Response contains header: X-Cache andX-Cache-detail

Eg:

X-Cache

MISS from www.mydomain.com

X-Cache-Detail

"cache miss: attempting entity  save" from www.mydomain.com

 

This let you have a clear picture ofwhether cache hits the reason if not hit

 

Enable logging

[httpd.conf]

LogLevel debug

 

CustomLog logs/cached-requests.log commonenv=cache-hit

CustomLog logs/uncached-requests.log commonenv=cache-miss

CustomLog logs/revalidated-requests.logcommon env=cache-revalidate

CustomLog logs/invalidated-requests.logcommon env=cache-invalidate

 

LogFormat "%{cache-status}e "cachelog

 

CustomLog logs/cache.log cachelog

 

 

Sample configuration

CacheRoot /tmp/cache

CacheLastModifiedFactor 0.5

CacheMaxExpire 300

CacheIgnoreHeaders Set-Cookie

CacheIgnoreNoLastMod on

CacheDefaultExpire 300

CacheIgnoreCacheControl on

 

CacheEnable disk /test/

CacheDirLevels 5

CacheDirLength 3

CacheMaxFileSize 1000000

CacheMinFileSize 64

#CacheLock on

#CacheLockPath /tmp/mod_cache-lock

 

SetEnvIf REQUEST_URI admin no-cache



转自:http://my.oschina.net/stubhub/blog/285869

相关问答

更多
  • 我看到你有DocumentRoot "/Users/myusername/Sites"但是块在etc/apache2/users/myusername.conf 我在http.conf不到对etc/apache2/users/myusername.conf任何引用,所以我首先检查该文件是否确实作为配置文件加载。 最简单的方法(但最具破坏性的,如果它是一个实时服务器)是在该文件中引入语法错误并重新加载服务器。 I see you have DocumentRoot "/Users/myu ...
  • 不幸的是,这是一个已知的限制,我们不得不退后使用mod_headers 。 Unfortunately, it's a known limitation and we had to fall back to use only mod_headers.
  • 在Apache2.2中,默认情况下, apache/conf/httpd.conf的以下行被取消注释。 LoadModule socache_shmcb_module modules / mod_socache_shmcb.so 从Apache 2.4以上的行被注释,所以删除它前面的#号。 这应该工作。 In Apache2.2 following line is uncommented in apache/conf/httpd.conf by default. LoadModule socache_shm ...
  • 您应该在评论中访问Rohan所述的http://192.168.23.73/mysite/ns_list/ 。 这是因为在Apache配置中你有 您可以在此处阅读有关location指令的更多信息。 来自文档: 在下面的示例中,使用尾部斜杠,对/ private2 /和/private2/file.txt的请求将应用所附的指令,但/ private2和/ private2other不会。 # ...
  • 这个目录容器: # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that " ...
  • 我建议使用mod_disk_cache,因为它比mod_mem_cache更快。 请尝试以下设置。 LoadModule cache_disk_module /usr/lib/apache2/modules/mod_cache_disk.so CacheDefaultExpire 3600 CacheEnable disk / CacheRoot "/path/to/cache/folder/" Cache ...
  • 最后,我使用guava写了自己的缓存: public class Cfg { private static Logger log = LoggerFactory.getLogger(Cfg.class); private Configuration cfg; private LoadingCache boolCache; private LoadingCache stringCache; private ...
  • 这不是我认为的优雅方式,而是对于能够遇到类似情况的人。 尝试修改/opt/elasticbeanstalk/hooks文件夹中的config.py文件。 您可以找到APACHE_TEMPLATE变量。 请将您自己的配置添加到此字符串中。 并上传您的新S / W版本。 就这样。 更新:我找到了更好的解决方案: - 如何在弹性beanstalk上强制https? This is not elegant way I think, but for someone who can met similar situat ...
  • 根据您的日志/audio/是现有目录。 所以你的规则不会运行,因为第一个条件会阻止它在现有目录上运行。 删除第一个条件来解决它。 此外,要在现有目录上执行此操作,您需要关闭DirectorySlash ,否则服务器将自动添加斜杠。 请注意,存在一些安全问题 。 According to your log /audio/ is an existing directory. So your rule wouldn't run since the first condition stops it running ...
  • 我不确定你为什么要去/~bangwsgi/ 。 您的WSGI应用程序正在/ ,由WSGIScriptAlias的第一个参数定义。 It works on removing the line WSGIPythonPath /home/bangwsgi/check And changing Require all granted to Allow from all