LEMP Field Notes
Nginx & PHP

FastCGI Cache, Varnish, or Application Cache: Which Layer for a PHP Site

FastCGI Cache, Varnish, or Application Cache: Which Layer for a PHP Site
In briefUse Nginx FastCGI cache for shared PHP responses when Nginx already fronts the site and you can prove every personalized bypass. Add Varnish when its HTTP policy and independent proxy tier justify another service. Use an application or object cache when PHP must run but repeatedly computes or queries the same data. The layers can coexist, but every key, bypass, invalidation event, failure mode, and owner must be documented.

Should a PHP site use FastCGI cache, Varnish, or an application cache?

Use FastCGI cache when Nginx already fronts a mostly public PHP site and you need full-response caching without another daemon. Add Varnish when its HTTP cache policy and operational tooling justify another proxy layer. Use an application or object cache when the expensive work is inside PHP or database queries and responses must remain personalized. These layers solve different problems; a site may use more than one, but each added cache needs an owner, invalidation rules, observability, and a safe bypass.

This is a capability and operational-cost comparison, not a benchmark. No universal hit rate, latency improvement, or traffic multiple is claimed.

What does each cache actually store?

A full-page cache stores an HTTP response under a cache key. On a hit, it can avoid running PHP and repeating the database work that produced the page. Nginx FastCGI cache and Varnish both operate at this response layer, although they occupy different positions and use different configuration models.

An application cache stores values chosen by the application: a database-query result, computed object, rendered fragment, session-independent lookup, or other reusable data. PHP still runs, but it may skip the expensive operation. WordPress, for example, documents its object cache as a way to save trips to the database. Its default cache lasts only for the request unless a persistent cache implementation is installed.

Layer Primary cached unit Can bypass PHP on a hit? Main control plane Added service?
Nginx FastCGI cache Complete FastCGI response Yes Nginx configuration and response headers No separate cache daemon
Varnish Complete HTTP response Yes Varnish Configuration Language and HTTP metadata Yes
Application/object cache Values selected by application code No Application, plugin, library, and cache backend Often

The table is architectural. It does not say which implementation will be faster for a particular site.

When is Nginx FastCGI cache the simplest fit?

Nginx's official FastCGI module includes cache-path, cache-key, bypass, no-cache, validity, locking, revalidation, and stale-response controls. It can cache responses returned by PHP-FPM and serve a valid cached response without sending that request back to PHP.

That makes it a practical first response cache when:

The difficult part is not creating a cache directory. It is proving the key and bypass policy. Host, scheme, path, query parameters, language, device variation, authentication state, and application cookies may change the correct response. If a relevant dimension is absent from the key or bypass rules, one visitor can receive another state.

Nginx documents that responses carrying Set-Cookie are not cached by default and that response headers such as X-Accel-Expires, Expires, and Cache-Control can affect caching. Treat those defaults as inputs to a design review, not as proof that every personalized response is excluded.

When does Varnish earn the extra layer?

Varnish Cache is a caching HTTP reverse proxy. It belongs between a TLS termination layer and an HTTP backend in a common deployment model. Its documentation illustrates a chain with TLS, Varnish, and backend as separate stages.

Varnish becomes attractive when the team wants its HTTP-oriented policy language, cache inspection, backend selection, grace behavior, or an independent cache tier shared across backends. It may also fit an organization that already operates Varnish consistently across applications.

The cost is another production service and another request boundary. The team must own:

Do not add Varnish merely because it is described as fast. If Nginx FastCGI cache already satisfies the response-cache policy, a second proxy may add more failure modes than value.

When is an application or object cache the right layer?

Choose an application cache when the page cannot safely be shared as one HTTP response but expensive sub-results can be reused. Common cases include authenticated dashboards, personalized pages, APIs assembled from several queries, and templates where only part of the output changes per user.

The application understands the data dependency, so it can use a key tied to the relevant object, locale, permissions, version, or tenant. It can also invalidate that key when the source changes. That precision is its advantage and its responsibility.

Redis is one possible backend, not a synonym for application caching. Redis documents that a cache can evict keys when memory exceeds a configured limit and that eviction policy determines which keys are removed. The application must therefore tolerate misses and rebuild values correctly. If a value cannot be regenerated from an authoritative store, it is not merely a cache entry.

For WordPress, a persistent object cache requires an appropriate drop-in or plugin and backend; setting an old configuration constant alone does not make the current object cache persistent. Other PHP frameworks have their own adapters, namespaces, serialization, and invalidation behavior. Follow the exact application documentation.

Can you combine these caching layers?

Yes, when each layer has a distinct job. A public article page might be served from FastCGI cache, while misses run PHP and reuse cached database objects. A larger platform might put Varnish in front of several application backends and still use application caches for personalized work.

The combination is safe only if invalidation crosses every layer. Updating content in the database may require application-object invalidation and response-cache purge. A response cache must never store logged-in, account, checkout, or permission-dependent output unless the application has deliberately designed a correct private-cache model.

Write a cache map before enabling anything:

  1. Name the object or response stored at each layer.
  2. Define the complete cache key and all variation dimensions.
  3. List requests and responses that must bypass storage and lookup.
  4. Name the event that invalidates or expires each entry.
  5. Define behavior on a cold cache, stale data, backend failure, and cache failure.
  6. Expose a safe diagnostic that shows hit, miss, bypass, and stale states.
  7. Assign the configuration, purge path, and incident response to an owner.

How does TLS affect the decision?

Nginx can terminate HTTPS and use FastCGI cache inside the same web-server layer. A Varnish design commonly terminates TLS before Varnish, then forwards HTTP plus the protocol and client context the backend requires. That makes proxy trust and header normalization explicit architecture decisions.

Do not accept forwarded client or scheme headers from an untrusted source. Define which proxy is trusted to set them, replace untrusted inbound values at the boundary, and test redirect, secure-cookie, canonical-host, and client-address behavior through the full chain.

An application cache is below HTTP termination. TLS does not change its cached object directly, but scheme, host, tenant, and permissions may still belong in application cache keys.

Which cache should you choose first?

Start with the measured bottleneck:

Establish a baseline, change one layer, and compare identical requests at matched load. Record correctness, hit and bypass states, PHP work, database work, errors, memory, disk, and recovery after purge or restart. A high hit ratio is not success if it serves stale or private content.

For sites hosted in Nginx server blocks, document cache policy beside the site's routing ownership; the Nginx server-block guide explains that boundary.

Sources

FAQ

Is Nginx FastCGI cache the same as Redis?

No. FastCGI cache stores complete responses returned by PHP-FPM and can bypass PHP on a hit. Redis is a data service that an application or cache plugin may use to store selected objects or computed values. PHP normally still runs on an object-cache hit. Choose based on the work you need to avoid, not the product name.

Do I need Varnish if Nginx caching is enabled?

Not automatically. Varnish adds an independent HTTP proxy and policy layer, plus configuration, TLS placement, forwarding, logging, health, upgrade, and purge responsibilities. If Nginx FastCGI cache already meets the site's response-cache and invalidation requirements, another proxy may not justify its operational cost. Add Varnish only for a specific capability your team will operate.

Can a site use page cache and object cache together?

Yes. The response cache can serve shared pages without PHP, while requests that reach PHP can reuse cached objects or query results. Document both layers. A content change may need to invalidate the object and every affected response, and personalized output must bypass any shared response cache unless the application implements a proven private-cache design.

How do I compare caching layers fairly?

Measure the current bottleneck, enable one layer, and repeat identical requests at matched load. Record correctness, cache hit, miss, bypass and stale states, PHP and database work, errors, resource use, and behavior after purge or restart. Do not judge by hit ratio alone: a cache that returns stale or private content is incorrect even when every request is fast.