Skip to content
hostcomplex

NGINX FastCGI Cache

Maarten Soetens 3 min read

How NGINX Caches Responses From PHP

NGINX FastCGI cache is a built-in caching feature of the NGINX web server. It stores responses that NGINX receives from a FastCGI backend, such as PHP-FPM, and serves later identical requests from its own cache. The web server itself becomes the page cache, without a separate caching program.

FastCGI is the protocol NGINX uses to pass requests to PHP processes. Without caching, each request is handed to PHP, which runs the application and returns HTML. With FastCGI caching, NGINX checks its stored copies first. When a valid entry exists, PHP is not called at all, which saves processor time and leaves the application free for requests that need it. NGINX can also act as a reverse proxy in front of PHP.

How the Cache Is Set Up in Principle

The cache needs a storage area, a key that identifies each response and a lifetime for entries. The key usually includes the request method and the host and path, so different pages do not overwrite each other. Rules also decide which requests are cached. Logged-in users, POST requests and responses that set certain cookies are commonly excluded, as personal pages are in other caches.

Many setups can also serve slightly stale content while the cache refreshes in the background, which keeps pages fast while the application is busy. Behavior is set with configuration directives such as fastcgi_cache_path rather than a graphical interface. This gives precise control, but it requires careful setup and testing.

NGINX FastCGI Cache Compared With Varnish

Both tools sit in front of an application and cache HTTP responses, so they are often compared. Varnish is a dedicated caching proxy with its own configuration language and rich rules for cache behavior. NGINX is a general web server and reverse proxy that can cache as one of many functions. Varnish is often chosen when caching is the main job. NGINX FastCGI cache suits sites where NGINX already handles serving and HTTPS.

A practical difference is that NGINX can handle encryption and caching in one component, which simplifies the stack for many sites. Varnish, in its open-source form, needs a separate component for HTTPS. Neither tool is faster in every case. The result depends on the workload and on the rules written.

Frequently asked questions

Is NGINX FastCGI Cache Good for WordPress?

It can be. Pages that look the same for most visitors can be cached at the web server level, which avoids PHP for those requests. Sites with heavy personalization need careful bypass rules, and some plugins that depend on PHP hooks may need adjustment. Whether it fits depends on how much control the site owner or host provides over server configuration.

How Is It Different From a Page Cache Plugin?

It is a type of page caching, but it works at the web server layer rather than inside PHP. Because the check happens before PHP runs, it can be faster than a plugin that loads PHP just to serve a saved file. The trade-off is that the rules live in server configuration, which many site owners cannot edit on shared hosting.

  • Two-Factor Authentication

    Two factor authentication hosting adds a second check to control panel, domain and CMS logins. Learn which methods are strongest and why 2FA helps.

  • What Is an A Record?

    What is an A record? Learn how this DNS record maps a hostname to an IPv4 address, why domains use several A records and the root and www pattern.

  • Billing and Automation Platforms

    What is WHMCS? Learn how this commercial billing and automation platform helps hosts sell plans, collect payments and create accounts automatically.