How does Nginx reverse proxy cache work?

mimipc asked:

I don’t understand how a reverse proxy with caching works.

Let’s assume that the cache is empty.

User1 loads index.php (dynamic content). There’s no cache, so Nginx queries the php file. The server says “Welcome User1”. Nginx stores the webpage using an md5 checksum.

User2 loads index.php. There is a page stored in the cache, so we return this page to the user. It says “Welcome User1”, but if we had called the php script, it would have returned “Welcome User2”.

How can Nginx know wether or not to call the php script again?
My reverse proxy with caching works great, returning dynamic content, but I imagine it’s not just “magic”.

Is it the php script which returns a header telling Nginx not to cache?

My answer:


Response caching is controlled by the HTTP Cache-Control response header.

If it is set to no-cache or private, then your reverse proxy will not cache the document. Typically private is used for pages sent to logged-in users; it means that proxies must not cache the document, but it’s OK for the web browser to do so.

You can see complete details on Cache-Control in RFC 2616 section 14.9 et seq.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.