2.4.1.1.13. Server caching
Important points:
- When enabling caching, ensure that the site returns caching prohibition headers when working with its admin panel, otherwise such pages may be cached and become accessible to unauthorized users.
- When using sessions, PHP automatically adds the
Cache-Controlheader to responses, and caching does not work by default. For caching to work, the cache limiter value must bepublic(see session_cache_limiter).
How caching works
When using caching, all server responses are stored in the cache, and when receiving new requests, instead of forwarding them to the site scripts for processing, a ready-made response is returned from the cache. Using caching helps to significantly reduce the load on the web server.
The page is not stored in the cache:
- If the request contains:
- The
Cookieheader with thenocachekey with a non-empty value and not equal to0. - GET parameter
nocachewith a non-empty value and not equal to0. - The
Authorizationheader with a non-empty value and not equal to0.
- If the response contains:
- The
X-Accel-Expiresheader with a value of0. - The
Set-Cookieheader. - The
Varyheader with the special value*. - The
Cache-Controlheader with any of the following values:max-age=0,private,no-store,no-cache.
- If the "output_buffering" parameter is disabled in the "PHP settings" section (when buffering is disabled, nginx does not process data from the web server and cannot cache it).
The existing cache is ignored if the request contains the same headers/parameters that prevent the page from being cached (see above). ⚠️ The cache is not overwritten or deleted. For example, when request A was made, the page was stored in the cache, then request B was made with conditions for ignoring the cache — the cache was ignored and the page was returned by the site, if request C is made without conditions for ignoring the cache — the cache that was generated for request A will be returned.
Using the X-Cache-Status response header, you can monitor cache usage:
- Page was returned by the site:
MISS— page was not in the cache.BYPASS— cache was ignored (see above).EXPIRED— cache lifetime has expired.
- Page was returned from cache:
STALE— cache lifetime has expired, but the current version of the page could not be obtained from the site.REVALIDATED— cache lifetime has expired, but it was revalidated using theIf-Modified-SinceorIf-None-Matchheader.UPDATING— cache lifetime has expired, but the page is being updated from the site.HIT— page was returned from cache.
When the cache is ignored and the response contains the Cache-Control header, the status may be appended with an indication of the reason why the cache was ignored: /PRIVATE, /NO-CACHE, /NO-STORE, /MAX-AGE.
Caching settings
Server caching is configured in the "Site settings" section on the "Caching" tab:
- Modes:
- "Disabled" — caching is disabled.
- "Enabled" — caching with GET parameters (for requests to the same page with different GET parameters and without them, different responses will be returned from the cache).
- "Ignore request parameters" — caching with GET parameters ignored (for requests to the same page with different GET parameters or without them, the same response will be returned from the cache).
- "Lifetime" — how many minutes after the last use cache will be deleted.
- Selected individually. For sites with frequent content updates, you can use lower values; for sites with infrequent updates, you can use higher values.
Clear cache
In the "Site settings" section, on the "Caching" tab, click "Clear cache".