Symfony HTTP Cache Reverse Proxy Configuration for User‑Specific Content
0 reputation · 12 Mar 2024, 13:47 UTC
0 reputation · 12 Mar 2024, 13:47 UTC
The goal is to configure Symfony’s built‑in HTTP cache (HttpCache) so that pages containing user‑specific data are never stored by a shared reverse proxy, while still allowing anonymous visitors to benefit from browser and proxy caching.
Symfony’s framework.yaml lets you set default Cache‑Control headers, but the documentation does not specify how to apply the 'private' directive conditionally based on authentication status, nor whether to pair it with 's‑maxage=0' for shared caches versus 'maxage' for browsers. This leaves an unresolved decision about the correct header combination that prevents stale private data from being served.
What is the recommended way to differentiate Cache‑Control directives for anonymous versus authenticated users in Symfony’s HttpCache? Should the 'private' directive be used alone, or combined with 's‑maxage=0' to explicitly forbid shared caching? How can we verify that the reverse proxy respects these headers without exposing private data?
34775 reputation · 12 Mar 2024, 21:06 UTC
Symfony's HttpCache acts as a shared reverse proxy; it obeys Cache-Control and Vary headers from the backend. To prevent user-specific content from being stored in a shared cache while allowing anonymous visitors to benefit from caching, configure conditional headers based on authentication status.
Cache-Control: public, max-age=60, s-maxage=300Cache-Control: private, no-store, must-revalidate, s-maxage=0The private directive prevents shared proxies from storing the response, while s-maxage=0 explicitly denies shared caching as a defensive measure. Use no-store for highly sensitive data to prevent browser caching. Always include Vary: Cookie, Authorization on private responses to ensure proper cache key differentiation.
$event->getRequest()->getUser().s-maxage=0.Verify the configuration without exposing private data by:
curl -I directly against the backend to confirm header emission.X-Symfony-Cache: MISS appears each time.Vary: Cookie is present on private responses and absent on public ones.private alone is RFC-compliant for preventing shared caching, but combining it with s-maxage=0 is a defensive best practice that explicitly forbids shared storage and is clearer to operators and legacy proxies. The no-store directive is required for highly sensitive data because private still permits browser storage; choose based on your risk tolerance.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.