cache_module usage
[1] LoadModule
00-base.conf
LoadModule cache_module modules/mod_cache.so LoadModule cache_disk_module modules/mod_cache_disk.so LoadModule headers_module modules/mod_headers.so
[2] 建立 cache 的 folder
mkdir /var/spool/apache-cache
chown apache:apache /var/spool/apache-cache
chmod 750 /var/spool/apache-cache
[3] Setting
# Proxy Settng ProxyPassMatch ^/favicon.ico$ https://backend/ ProxyPassMatch ^/.* https://backend/path-to-folder # Cache Settng CacheQuickHandler off CacheLock on CacheRoot "/var/spool/apache-cache" <LocationMatch "\.(txt|ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> CacheEnable disk Header set Cache-Control "max-age=600, public" </LocationMatch> CacheMaxFileSize 10485760 # Info CacheHeader on # Cache control (Client -> Server) CacheIgnoreCacheControl On CacheIgnoreNoLastMod On CacheDefaultExpire 600 CacheIgnoreQueryString Off # Ignoring Upstream Server Caching Headers Header unset Expires Header unset Cache-Control Header unset Pragma # Improve performance when caching content from a reverse proxy CacheReadSize 102400 CacheReadTime 1000
CacheEnable
Enable caching of specified URLs using a specified storage manager
Syntax: CacheEnable cache_type [url-string]
- disk => mod_cache_disk
- socache => mod_cache_socache
# Cache control (Client -> Server)
CacheIgnoreCacheControl On
CacheIgnoreNoLastMod On
CacheDefaultExpire 600
CacheIgnoreQueryString Off
CacheMaxFileSize
Unit: bytes, 10485760 = 10M
CacheQuickHandler
Default: on
On: This phase short circuits the majority of server processing,
and represents the most performant mode of operation for a typical server.
Off: the cache operates as a normal handler, and is subject to the full set of phases when handling a server request.
While this mode is slower than the default,
it allows the cache to be used in cases where full processing is required,
such as when content is subject to authorization.
This module should be used with care, as when the CacheQuickHandler directive is in its default value of on,
the Allow and Deny directives will be circumvented.
off: it becomes possible to insert the CACHE filter at a point in the filter stack chosen by the administrator.
(ie. mod_deflate filter)
CacheLock
Default: off
The "CacheLockMaxAge directive" specifies the maximum age of any cache lock.(Default: 5)
A lock older than this value in seconds will be ignored, and the next incoming request will be given the opportunity to re-establish the lock.
This mechanism prevents a slow client taking an excessively long time to refresh an entity.
CacheIgnoreCacheControl # Default: Off
Ignore request to not serve cached content to client
requests containing a Cache-Control: no-cache or Pragma: no-cache header value will not be served from the cache.
On: tells the server to attempt to serve the resource from the cache even if the request contains no-cache header values.
CacheIgnoreNoLastMod # Default: Off
Ordinarily, documents without a last-modified date are not cached.
The CacheIgnoreNoLastMod directive provides a way to specify that documents without
last-modified dates should be considered for caching, even without a last-modified date.
If neither a last-modified date nor an expiry date are provided with the document
then the value specified by the CacheDefaultExpire directive will be used to generate an expiration date.
CacheDefaultExpire # Default: 3600
CacheMaxExpire
cacheable HTTP documents will be retained without checking the origin server.
This maximum value is enforced even if an expiry date was supplied with the document.
CacheIgnoreQueryString # Default: Off
Ordinarily, requests with query string parameters are cached separately for each unique query string.
# Improve performance when caching content from a reverse proxy
CacheReadSize
amount of data to be read from the backend before the data is sent to the client.
CacheReadTime
During the time period, data will be buffered before sending the result to the client.
# Ignoring Upstream Server Caching Headers
Header unset Expires Header unset Cache-Control Header unset Pragma
因為 Upstream 有 "Cache-Control: no-store, no-cache" 時. 就永遠 X-Cache: MISS
CacheHeader
# Enable the X-Cache header (HIT, REVALIDATE, MISS)
CacheHeader on
* 不受 cache 影響的不會有此 header. (因為它們不是 HIT/MISS)
REVALIDATE The entity was stale, was successfully revalidated and was served from cache.
Remark
* Disabling Caching for certain Paths
<LocationMatch "^/api/v1/$"> CacheDisable on </LocationMatch>
* service httpd reload 不會令 cache 過期
Cache Data File
ls -R /var/spool/apache-cache
???.data # 原本的 data ???.header # DBase 3 index file
Testing
想查看 header, 在 curl 不可以用 "-I", 因為那是 HEAD 來, 不會 cache 到 cache
n.n.n.n - - [23/Jul/2021:13:10:51 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.64.0"
要用 -D
curl -k -D headers.txt https://datahunter.org/test.txt > test.txt; cat headers.txt
Other Setting
CacheDetailHeader
on => X-Cache-Detail: "conditional cache hit: entity refreshed" from localhost
Set Cache-Control
* 必須要設定 cache 什麼, 因為 request "/" 的 response header 有 "Set-Cookie: ", 這會有安全問題 !!
<FilesMatch "\.(txt|ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=600, public" </FilesMatch>
# 用 LocationMatch 有相同效果
<LocationMatch "\.(txt|ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=600, public" CacheEnable disk </LocationMatch> CacheRoot "/var/spool/apache-cache"
* 用 LocationMatch 是因為 CacheEnable 不支援 FilesMatech
http Age response header
The Age field value is the cache's estimate of the amount of time since the response was generated or revalidated by the origin server.
The latter can bereliably calculated as
Expires: - Date:, or from Cache-control: max-age=NNN (which takes priority).
Cache-Control vs Expires vs Pragma
* Cache-Control 已經可以取代其他, 所以用佢已經夠了
Cache-Control
was defined in HTTP/1.1, tells all caching mechanisms from server to client whether they may cache this object.
Expires
The Expires header field gives the date/time after which the response is considered stale.
Invalid dates, like the value 0, represent a date in the past and mean that the resource is already expired.
Pragma
Use Pragma only for backwards compatibility with HTTP/1.0 clients.
If a response includes a Cache-Control field with the max-age directive, a recipient MUST ignore the Expires field.
i.e.
Header unset Expires Header unset Pragma <LocationMatch "\.(txt|ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=600, public" CacheEnable disk </LocationMatch>
Cache-Control 的 public
The response may be stored by any cache, even if the response is normally non-cacheable.
解決重複出現 header
Client <- Prxoy (Header add ...) <- Backend (Header add ...)
i.e.
X-Frame-Options: SAMEORIGIN X-Frame-Options: SAMEORIGIN
# remove header(backend->proxy)
Header unset X-Frame-Options Header unset Referrer-Policy Header unset Strict-Transport-Security Header unset X-Powered-By
htcacheclean
將 cache size 限制在一定數量內
i.e.
htcacheclean -n -t -p /var/spool/apache-cache -l 512M
-n Be nice. This causes slower processing in favour of other processes
-t Delete all empty directories.