Performance

When PHP Goes Down, Keep Serving Pages: Stale Cache, 503s and the Outage Google Never Sees

Make nginx serve stale cached pages when PHP-FPM is down: the fastcgi_cache_use_stale values nginx accepts, the http_503 trap and the limits.

Varun DubeyVarun Dubey
·15 min read
Terminal log showing a cached page served as 200 STALE while PHP-FPM is stopped and an uncached page returns 502

To make nginx keep serving pages when PHP-FPM is down, add fastcgi_cache_use_stale error timeout updating http_500; to the PHP location that already has fastcgi_cache, switch on fastcgi_cache_background_update and fastcgi_cache_lock, and raise inactive= on fastcgi_cache_path so cached copies stay on disk through a long outage. Only URLs that already sit in the cache survive. Everything else, and every logged-in visitor, still sees the error.

We broke a WordPress 7.1.3 site behind nginx’s FastCGI cache nine different ways (results below, dated 17 October 2026) and, for this refresh, re-checked every directive against the nginx.org documentation on 15 November 2026 and ran nginx -t on the final config with nginx 1.30.6. This is not a cache setup guide. If the FastCGI cache is not running yet, start with our nginx FastCGI cache setup and purge guide and come back.

How do I make nginx serve cached pages when PHP is down?

Inside the PHP location block that already has fastcgi_cache, add three lines. Config checked on 15 November 2026.

fastcgi_cache_use_stale error timeout updating http_500;
fastcgi_cache_background_update on;
fastcgi_cache_lock on;

Then make the inactive= value on your fastcgi_cache_path line as long as the outage you want to survive. In our October tests those lines turned every PHP, database and timeout failure on an already-cached page into a normal 200 response, and the hung-PHP case went from a 10-second wait to an instant answer.

Leave http_503 out of that list unless you have thought about it. With it, WordPress’s own maintenance mode stops working for every cached page. The test results below show why.

Which fastcgi_cache_use_stale values exist, and which does nginx refuse?

The nginx FastCGI module documents this syntax (checked 15 November 2026): fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | http_429 | off ...; The default is off, and it works in the http, server and location contexts.

Notice what is missing: http_502 and http_504. Those two exist only in the proxy version of the directive. We confirmed it. On 15 November 2026 we added http_502, and separately http_504, to an otherwise valid config and ran nginx -t on nginx 1.30.6. Both failed with [emerg] invalid value "http_502" (and the same message for http_504), and the config test reported failure.

Keyword

What it covers

Worth it for WordPress?

error

nginx could not pick or reach a PHP-FPM server (stopped, crashed, connection refused)

Yes, this is the stopped-PHP case

timeout

The connection or the read timed out (PHP accepted the request but did not answer in time)

Yes, this is the hung-PHP case

invalid_header

PHP returned an empty or invalid response header

Rarely needed, but harmless

updating

The cached item is already being refreshed by another request

Yes, and required for background updates

http_500

PHP answered with status 500

Yes, this covers the database-down error page

http_503

PHP answered with status 503

Only on purpose, see the maintenance section

http_403, http_404, http_429

PHP answered with that status

No. Hiding a real 404 or a rate limit behind an old page causes confusion

off

Never serve stale

The default

What is the proxy_cache_use_stale equivalent?

If nginx talks to PHP through proxy_pass (for example a Docker setup with an Apache or Varnish layer in between), you use the proxy_* twins. They behave the same way, and their documented syntax is a superset: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...;

FastCGI (PHP-FPM direct)

Proxy (upstream over HTTP)

Difference

fastcgi_cache_use_stale

proxy_cache_use_stale

Only the proxy version accepts http_502 and http_504

fastcgi_cache_background_update

proxy_cache_background_update

Both default to off and need updating in the stale list

fastcgi_cache_lock

proxy_cache_lock

Same behaviour, same 1.1.12 introduction

fastcgi_cache_valid

proxy_cache_valid

Same syntax and the same “only 200, 301 and 302 when no codes are given” rule

If you copy a snippet from a proxy tutorial into a FastCGI block, drop http_502 and http_504 or nginx will not start.

Does nginx serve stale cache by default when PHP is down?

No. The default is fastcgi_cache_use_stale off. A cached page only survives an outage while it is still fresh, and the moment its lifetime ends nginx goes back to PHP, finds nothing there and passes the error on. We measured this on 17 October 2026.

How we tested

The test stack ran in Docker on one machine:

  • WordPress 7.1.3 on the official wordpress:php8.3-fpm image (PHP 8.3.35)
  • nginx 1.27.5 with a FastCGI cache and an X-Cache header showing $upstream_cache_status
  • MariaDB 11
  • Pages cached for 10 seconds (fastcgi_cache_valid 200 301 10s) so we could test expired entries without waiting an hour
  • Logged-in users bypass the cache through the usual wordpress_logged_in cookie check

The base block looked like this:

fastcgi_cache_path /var/cache/nginx/wp levels=1:2 keys_zone=WP:10m inactive=60m max_size=100m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
map $http_cookie $skip_cache { default 0; ~wordpress_logged_in 1; ~comment_author 1; }

server {
  # ...
  add_header X-Cache $upstream_cache_status always;

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass wp:9000;
    fastcgi_connect_timeout 3s;
    fastcgi_read_timeout 10s;
    fastcgi_cache WP;
    fastcgi_cache_valid 200 301 10s;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    # stale directives go here
  }
}

Each outage was real: we killed the PHP-FPM master (connections refused), froze the PHP container (connections accepted, no answer), stopped MariaDB (WordPress returns its database error page with status 500) and created the .maintenance file (WordPress returns 503).

One trap from that session: after an nginx -s reload our first run produced results that made no sense, including stale pages from a config with no stale directive at all. A full restart of nginx between configs gave clean, repeatable results. If your own tests look impossible, restart rather than reload before trusting them.

What each setup served

Three configurations, nine scenarios. “Fresh” means the cached copy was inside its 10-second lifetime and “expired” means it was older.

  • A: no stale directives (the nginx default)
  • B: fastcgi_cache_use_stale error timeout updating http_500 http_503;
  • C: fastcgi_cache_use_stale error timeout updating http_500; (no http_503)

Scenario

A: default

B: with http_503

C: without http_503

Fresh cached page, PHP-FPM stopped

200 HIT

200 HIT

200 HIT

Expired cached page, PHP-FPM stopped

502

200 STALE

200 STALE

Page never cached, PHP-FPM stopped

502

502

502

Logged-in visitor, PHP-FPM stopped

502

502

502

Expired page, PHP-FPM hung

504 after 10 s

200 STALE after 10 s

200 STALE after 10 s

Expired page, database down

500

200 STALE

200 STALE

Expired page, WordPress maintenance mode

503 + Retry-After: 600

200 STALE

503 + Retry-After: 600

Uncached page, WordPress maintenance mode

503 + Retry-After: 600

503 + Retry-After: 600

503 + Retry-After: 600

The default setup is the problem. On a site that caches pages for an hour, an hour-long outage takes down every cached page by the end of it.

Why is hung PHP worse than dead PHP?

A stopped PHP-FPM fails in a few milliseconds, because nginx gets “connection refused” straight away. A hung PHP-FPM accepts the connection and then says nothing, so nginx waits for the full fastcgi_read_timeout (10 seconds in our test) before it gives up. With timeout in the stale list the visitor does get the page, but only after staring at a blank tab for ten seconds, and each of those requests holds an nginx connection open the whole time.

We tried two fixes on an expired page with PHP hung:

Setup

Result

Stale list only, fastcgi_read_timeout 10s

200 STALE after 10.0 s

Stale list only, fastcgi_read_timeout 3s

200 STALE after 3.0 s

Stale list + fastcgi_cache_background_update on + fastcgi_cache_lock on

200 STALE in 0.004 s, next request 200 UPDATING in 0.003 s

Lowering the read timeout is a blunt tool, since a legitimately slow admin export gets cut off too. Background updates are the better answer. The nginx documentation describes fastcgi_cache_background_update as starting a background subrequest to refresh an expired item while the stale copy goes to the client, and it states that using stale responses while an item is updating must be allowed. That is why updating has to be in the stale list. If PHP is hung, the background request waits, not your visitor.

What does fastcgi_cache_lock add?

With fastcgi_cache_lock on, only one request at a time may go to PHP to populate a given cache element, and the others wait for the result. Two companion directives set the limits, both from the same documentation page: fastcgi_cache_lock_timeout (default 5 seconds) and fastcgi_cache_lock_age (default 5 seconds). When the lock timeout expires, the waiting request is passed to PHP anyway, and since nginx 1.7.8 its response is not cached.

That detail matters for an outage. A page that has no cached copy at all and a hung PHP will make its waiting visitors queue for up to the lock timeout before each is let through to fail. The lock protects a struggling PHP from a stampede, but it does not turn a cold URL into a cached one. We did not test lock timeouts under a hung PHP, so treat the 5-second figure as documented behaviour, not a measurement.

Background updates also help when nothing is broken: with PHP healthy, the first request after expiry got the stale copy in 4 milliseconds and the next was a fresh HIT.

How long will nginx keep serving a stale page?

Until the cached file is removed, and two settings decide when that happens.

  • fastcgi_cache_valid: how long a page counts as fresh. With explicit codes, as in fastcgi_cache_valid 200 301 10m;, only those response codes are cached for that time. If you give only a time, nginx caches 200, 301 and 302 responses. Headers from the application take precedence: X-Accel-Expires, then Expires and Cache-Control. A response that sets a cookie is not cached.
  • inactive= on fastcgi_cache_path: cached data that is not accessed within this time is removed regardless of freshness. The documented default is 10 minutes, which is far too short for outage cover.

We set inactive=20s, let a page sit unrequested for 25 seconds, stopped PHP-FPM and got a 502 with X-Cache: MISS. The stale copy was gone.

Size matters too. When the cache reaches max_size, nginx’s cache manager removes the least recently used data. The documentation gives no default for it, so set it deliberately. A site with a few thousand posts typically needs a few hundred megabytes to a couple of gigabytes depending on page size. Check the real size of the cache directory after a week of traffic.

Why does WordPress maintenance mode stop working after I enable stale cache?

When WordPress updates a plugin, theme or core, it writes a .maintenance file and answers every request with a 503 and a Retry-After: 600 header. We confirmed both on 7.1.3. That 503 is the honest answer during an update.

Add http_503 to the stale list and nginx treats that honest 503 as a failure to hide. Every cached page keeps serving its stale copy (row “Expired page, WordPress maintenance mode”, column B). Only pages that were never cached show the maintenance message.

Whether that is acceptable depends on why the site is in maintenance:

  • Routine plugin updates: serving yesterday’s page for a minute is harmless, and arguably better than a maintenance screen. Some owners include http_503 on purpose.
  • A deliberate shutdown, for example to stop orders during a migration or to pull content that must not be shown: http_503 defeats you. Visitors keep reading cached pages, and some clicks land on uncached pages that do show maintenance, so the site looks half broken.

Our default is to leave http_503 out and decide per site. Whatever you choose, write it next to the config so the next person does not wonder why maintenance mode “does not work”.

What about logged-in users and cookies?

They never see stale pages, because they should never be served cached pages in the first place. The bypass in our config does two jobs. fastcgi_cache_bypass stops nginx taking the response from the cache, and fastcgi_no_cache stops it saving the response. nginx’s documentation says either directive applies when at least one of its values is non-empty and not “0”, which is why a map that returns 1 or 0 works.

# http block. Config checked on 15 November 2026.
map $http_cookie $skip_cache {
  default 0;
  ~wordpress_logged_in 1;
  ~comment_author 1;
  ~wp-postpass_ 1;
}

The wp-postpass_ line is new since our October run, so password-protected posts are not cached for people who entered the password.

The flip side is the real limit: during an outage, all of these requests go straight to PHP and see the error at full strength. On a membership or shop site that can be most of the people who matter. Our guide to cookies that bypass your WordPress page cache covers which cookies trigger the bypass and which ones you can safely ignore, which shrinks that group.

What can the stale cache never cover?

Two rows stayed red in every configuration, and no nginx setting fixes them:

  • Pages that were never cached. A visitor landing on an old post nobody has opened since the cache was cleared gets the error. To cover the whole site, warm the cache by requesting every URL in your sitemap after a deploy or purge.
  • Anything that bypasses the cache. Admins, members, customers, carts, search and anything with a bypass cookie.

Stale cache keeps the public face of the site up. It does not make the site work. Forms, checkout, search and comments still need PHP, so treat the cache as the thing that buys you time while you fix the real problem. If PHP-FPM keeps falling over under load, the cause is usually worker limits, and our PHP-FPM tuning guide shows how to calculate them.

How do I send a proper 503 for planned maintenance?

When you take the site down on purpose, the correct answer to visitors and crawlers is a 503 with a Retry-After header. RFC 9110 defines 503 as a temporary condition. Doing it in nginx rather than WordPress means it works even when PHP is the thing you are working on, and a flag file switches it on and off without editing the config. Config checked on 15 November 2026, and the syntax passed nginx -t on nginx 1.30.6.

server {
  # ...
  set $maint 0;
  if (-f $document_root/maintenance.on) { set $maint 1; }
  if ($remote_addr = 203.0.113.10) { set $maint 0; }   # your own IP keeps working

  error_page 503 @maint;
  location @maint {
    add_header Retry-After 3600 always;
    add_header Cache-Control "no-store" always;
    rewrite ^ /maintenance.html break;
  }

  location / {
    if ($maint) { return 503; }
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    if ($maint) { return 503; }
    # ... your FastCGI and cache settings
  }
}
# switch on
touch /var/www/html/maintenance.on
# switch off
rm /var/www/html/maintenance.on

One word matters more than any other in that block: always. Without it, add_header only applies to successful and redirect responses, so in our October test the 503 went out with no Retry-After header at all and no error in any log. With always it carried Retry-After: 3600 and Cache-Control: no-store. The maintenance page itself rendered the same either way, so you would never spot the missing header by looking at the page.

What does Google see during an outage?

Google’s documentation on HTTP and network errors says 5xx and 429 responses make Google’s crawlers slow down, and that the crawl rate recovers gradually once the server returns 2xx again. Pages that are already indexed are kept at first, but URLs that persistently return a server error are eventually removed from the index. The page gives no timeframe, and it does not mention Retry-After. Send the header for every other client, but do not count on it to protect your rankings.

A stale page served with status 200 is, to Googlebot, a normal page. During a short outage with the setup above, the crawler sees nothing wrong, which is the point. That cuts both ways, so you need monitoring.

How do I know when I am serving stale pages?

The setup that hides an outage from visitors hides it from you too. An uptime monitor that checks your homepage sees 200 and reports all is well while PHP has been down for an hour. Two cheap fixes:

  • Log the cache status. Add $upstream_cache_status to your nginx log_format. A burst of STALE or UPDATING lines is your outage alarm.
  • Monitor something that cannot be cached. Point the uptime check at a URL that always reaches PHP, such as /wp-login.php or a small health-check endpoint, so a PHP failure shows up as a failure.
log_format cache '$remote_addr [$time_local] "$request" $status $upstream_cache_status $request_time';
access_log /var/log/nginx/access.log cache;
# How many stale answers in the last 5000 log lines?
tail -n 5000 /var/log/nginx/access.log | grep -c ' STALE '

Does Cloudflare add a second safety net?

It can, but only for responses Cloudflare is caching, and by default it does not cache HTML pages unless a cache rule says so. We did not test Cloudflare in this setup. This is what its Cache-Control documentation said when we checked on 15 November 2026:

  • stale-if-error: triggers when the origin returns a 5xx status (500, 502, 503 or 504). Other statuses such as 404 replace the cached copy, and you cannot change which codes trigger it.
  • stale-while-revalidate: the first request after expiry gets the stale copy with an UPDATING status while Cloudflare revalidates in the background.
  • Both are ignored when Always Online is enabled, and when the response carries directives such as no-store, no-cache or must-revalidate.

Always Online is a different feature. It only activates when Cloudflare cannot connect to your origin at all (the 520 to 527 range). It looks for a stale copy in its own cache first and then falls back to static pages from the Internet Archive, which are crawled every 30 days on Free, 15 on Pro and 5 on Business and Enterprise. For a crashed PHP-FPM behind a working nginx, Always Online does nothing, because nginx is still answering. Our post on WordPress caching, layer by layer explains how the edge, server and plugin caches stack.

What config do you use?

Put together, with starting values to adjust for your site. The inactive, max_size and valid numbers are starting points, not test results. Config checked on 15 November 2026, and the full file (including the maintenance block above) passed nginx -t on nginx 1.30.6.

# http block
fastcgi_cache_path /var/cache/nginx/wp levels=1:2 keys_zone=WP:100m inactive=7d max_size=2g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

# PHP location block
fastcgi_cache WP;
fastcgi_cache_valid 200 301 10m;
fastcgi_cache_use_stale error timeout updating http_500;
fastcgi_cache_background_update on;
fastcgi_cache_lock on;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;

With these values a page is refreshed at most every 10 minutes while PHP is healthy, and a page anyone opened in the last seven days can be served for up to seven days if PHP stays down.

How do I test it safely?

Do this on staging, never on a busy live site. Start with the syntax check. It catches the http_502 mistake before it takes the site down:

sudo nginx -t && sudo systemctl reload nginx

We did not have a local nginx install, so we ran the same check inside a disposable container (nginx:stable-alpine, nginx 1.30.6, config mounted read-only). That proves the syntax only, not the stale behaviour. The behaviour test is the manual one:

  1. Add add_header X-Cache $upstream_cache_status always; to the server block and restart nginx.
  2. Request a public page twice and confirm the second response says X-Cache: HIT.
curl -s -o /dev/null -D - https://staging.example.com/sample-page/ | grep -i x-cache
  1. Wait until the page has expired (longer than your fastcgi_cache_valid).
  2. Stop PHP-FPM: sudo systemctl stop php8.3-fpm (the service name varies by host).
  3. Request the page again. You want 200 and X-Cache: STALE. A 502 means the stale directive is missing or the page fell out of the cache.
  4. Request a page you never opened. Expect a 502: that is the cold-cache gap.
  5. Start PHP-FPM again and confirm the next requests return to HIT.

If the results look impossible, do a full systemctl restart nginx and run the test again.

Common mistakes people report

These come from the nginx issue tracker, the nginx mailing list and a WordPress hosting forum. We paraphrase them and link each source. Checked on 15 November 2026. Several are old reports on old nginx versions, so test before assuming they still apply.

  1. Copying http_502 or http_504 from a proxy example. Proxy tutorials list them, the FastCGI directive does not accept them, and nginx refuses to start or reload. See the FastCGI module docs versus the proxy module docs. Our nginx -t run on 1.30.6 matched this.
  2. Forgetting updating when enabling background updates. The documentation says stale use while updating must be allowed. Without it the background subrequest does not do what people expect.
  3. A stale copy that never goes away. A thread on the Roots forum from January 2020 describes a post switched to private that stayed visible to logged-out visitors, with STALE in the cache header. One reply pointed at WordPress sending no-cache headers on some responses, and the poster’s workaround was caching any response code for a short time. A second poster in the same thread had a different cause: a plugin’s own transient cache, not nginx. Read the thread.
  4. A known gap with background updates and uncacheable responses. A 2017 ticket on nginx 1.13.4 reports that when the background refresh returns something the cache rules do not store (the example was a redirect), the expired entry is never replaced and nginx keeps returning STALE. The ticket was still marked new when we checked, so confirm your own redirects and error codes are covered by fastcgi_cache_valid. Ticket 1402.
  5. Slow downloads with an HTTP/1.0 hop in front. A 2017 ticket on nginx 1.13.3 reported that a STALE response arrived quickly but the client’s connection stayed open until the upstream finished. A maintainer said that behaviour is intentional. The reporter traced it to a proxy in front that was missing proxy_http_version 1.1. Ticket 1329.
  6. Expecting stale cache to beat an error_page. A 2014 mailing list post describes a setup where, with the backend down, clients got nginx’s default 500 page instead of the stale copy, and the poster’s debug log suggested error_page handling ran first. The post had no replies, so we cannot say this is a general rule. It is a reason to test with your own error_page lines in place. Mailing list post.

Checklist

  • fastcgi_cache_use_stale error timeout updating http_500; in the PHP location
  • No http_502 or http_504 in a FastCGI block, and nginx -t before every reload
  • http_503 left out, or included on purpose and written down
  • fastcgi_cache_background_update on; and fastcgi_cache_lock on;
  • inactive= set to the outage you want to survive, not the 10-minute default
  • max_size large enough for the pages you want covered
  • Cache warmed after deploys and purges
  • Maintenance 503 sent with Retry-After and add_header ... always
  • $upstream_cache_status in the access log, and an uptime check on an uncacheable URL
  • Tested on staging by actually stopping PHP-FPM

Frequently asked questions

Does nginx serve stale cache by default when PHP is down?

No. The default is fastcgi_cache_use_stale off, and in our test an expired cached page returned 502, 504 or 500 depending on the outage.

Why does nginx say invalid value “http_502”?

Because the FastCGI directive does not accept http_502 or http_504. Only proxy_cache_use_stale does. Use error for connection failures and timeout for slow answers in a FastCGI setup.

Why does WordPress maintenance mode stop working after I enabled stale cache?

Because http_503 is in your stale list. WordPress answers with a 503 during updates, and nginx replaces it with the stale cached page. Remove http_503 to get the maintenance message back.

Do logged-in users see the cached copy during an outage?

No. They bypass the cache, go straight to PHP and get the error.

Does Retry-After stop Google from dropping my pages?

Google’s documentation on server errors does not mention Retry-After. It says 5xx responses slow crawling and that URLs returning server errors persistently are eventually removed. Keeping outages short, and serving cached pages with status 200 while you fix them, is what protects your pages.

Is Cloudflare Always Online enough on its own?

Not for a PHP failure. Always Online only activates when Cloudflare cannot reach your server at all. If nginx is up and PHP is down, nginx answers with an error and Always Online does not step in.

How we tested and checked this

The nginx and WordPress behaviour results come from our own test stack on 17 October 2026: WordPress 7.1.3, PHP 8.3.35 (PHP-FPM), nginx 1.27.5 and MariaDB 11 in Docker. Each configuration was loaded with a full nginx restart, and each outage was produced for real by stopping PHP-FPM, freezing its container, stopping the database or creating WordPress’s .maintenance file. On 15 November 2026 we re-read the directive documentation on nginx.org and ran nginx -t (not a traffic test) against the final config and the invalid http_502 and http_504 variants with nginx 1.30.6. Cloudflare and Google behaviour is quoted from their own documentation and was not tested by us. We have not re-run the nine-scenario traffic test on nginx 1.30.

Want this set up and tested on your server? Our WordPress speed optimization service configures and load-tests server caching, our WordPress care plans include monitoring that notices when PHP is down even while the cache hides it, and if the site is down right now, get WordPress help.

Part of the Wbcom Designs family

The all-in-one WordPress community stack

Also ours: wbcomdesigns.comvapvarun.combrndle.com