Adding $upstream_cache_status in HTTP Response Headers

In our checklist for perfect WordPress-Nginx setup, we have a section dedicated to check if page-caching will work in case PHP/MySQL backend crash. Some readers did not like my way of actually shutting down PHP backend for cache-verification.

If you are using Nginx’sfastcgi_cache, then you can useupstream_cache_status variable to test cache for particular URLs without shutting down PHP.

All you need to do is, add a line like below to /etc/nginx/nginx.conf file, in http{..} block:

add_header rt-Fastcgi-Cache $upstream_cache_status;

Just reload nginx config: service nginx reload

Check HTTP response for any page and you will see something like:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 26 Jul 2013 15:38:51 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.17-1~precise+1
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
rt-Fastcgi-Cache: HIT

Please note last line. Value next to rt-Fastcgi-Cache indicates fastcgi-cache status.

If you see a MISS, send similar request again and it should get you a HIT.

If you see BYPASS, that means some conditions for skipping cache are met.

Related: Using $upstream_cache_status improving cache efficiency

10 responses to “Adding $upstream_cache_status in HTTP Response Headers”

  1. I get BYPASS for all pages except homepage. Why? I’ve followed your tutorial to set up fastcgi on LNMP

  2. Use nginx-helper plugin to remove index.php from permalinks –

    I think index.php in URL is reason for cache getting BYPASS.

    Also, you may need to save your wordpress-permalinks again after activating nginx-helper. Though you don’t need to make any changes in nginx-helper as activating it is enough to remove index.php from permalinks.

    • Now I get HIT, but there’s no following like output in the page source:

      <!-- Dynamic page generated in 0.050 seconds. -->
      <!-- Cached page generated by WP-Super-Cache on 2012-09-14 04:52:06 -->
      <!-- super cache -->


      • Both are different thing. HIT in curl output is enough to conclude cache is working properly.

        If you want to see HTML comments in output, go to nginx-helper plugin settings and turn on “Enable Nginx Timestamp in HTML” option.