PHP caching and session privacy
Choose a small public cache surface
FastCGI caching can save a generated public response near the client, reducing repeated PHP work. A cache entry is shared among matching requests, so eligibility is a security decision before it is a performance decision. A login, administration page, cart, preview, account page, or response based on identity is not part of this fixture cache. Start with one audited anonymous public route. A path allowlist is more conservative than trying to enumerate every sensitive plugin route.
map $request_uri $private_route { default 1; ~^/news/$ 0;}Classify request variants
The fixture uses a case-sensitive anchored regex on the original request target, including query parameters, so /news/?preview=1 is excluded even if routing ends at index.php. Every nonempty Cookie or Authorization header makes the request ineligible, including unfamiliar cookies. This sacrifices cache hit rate to simplify the trust model. GET and HEAD are allowed; requests that can change application state are excluded. WordPress and commerce plugins have their own cookie, session and route contracts; inspect them before broadening eligibility. A missing cookie is not proof of anonymity if some other identity mechanism exists.
map $request_method $private_method { default 1; GET 0; HEAD 0; }map $http_cookie $has_cookie { "" 0; default 1; }map $http_authorization $has_auth { "" 0; default 1; }Prevent both cache reads and writes
fastcgi_cache_bypass controls whether a stored response is used; fastcgi_no_cache controls whether the received response is stored. Apply the same exclusion flags to both. The fixed host guard prevents unrecognized hostnames; the key contains scheme, host and original target. The key alone cannot solve every representation variation. Keep origin Cache-Control, Set-Cookie and Vary handling intact; never ignore privacy directives just to improve hit rate. The origin marks only the audited public route as public and everything else private/no-store. A 30-second validity period is not an entitlement to cache private content.
fastcgi_cache fixture;fastcgi_cache_key "$scheme|$host|$request_uri";fastcgi_cache_valid 200 30s;fastcgi_cache_bypass $private_route $private_method $has_cookie $has_auth;fastcgi_no_cache $private_route $private_method $has_cookie $has_auth;Prove privacy separation before measuring hits
In an isolated lab with PHP-FPM configured, make two anonymous GETs to the public route, then a request with a synthetic cookie, then an anonymous request again. Expect MISS followed by HIT for a fresh cache, BYPASS for the cookie request, and the original public body again. Query and account routes should bypass; a bypassed response should not contaminate the public entry. HEAD may share the GET cache representation under the default conversion behavior. Record actual response status, body and cache marker; an observed HIT alone is not proof that the content is safe. The table is a planned sequence, not a measured result.
GET /news/ → MISS → public-newsGET /news/ → HIT → public-newsGET /news/ (Cookie: demo=1) → BYPASS → uncached-newsGET /news/ → HIT → public-newsGET /news/?preview=1 → BYPASS → uncached-newsUse the accompanying Chapter 47 manual practice and lab. Configuration snippets are excerpts, not complete production WordPress configurations. The complete fixture and expected results are provided separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
PHP caching and session privacy
Assess decisions from the lesson and the original lab.
Which request is eligible for the fixture cache?
What does fastcgi_cache_bypass control?
Which directive prevents storing an excluded response?
Why retain origin Set-Cookie and Cache-Control handling?
After a cookie-bearing request, what must the next anonymous response contain?