2

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?

mimipc
  • 1,947
  • 3
  • 19
  • 27
  • This answer may help explain nginx to you. It also mentions gunicorn, which is used to run python code, but maybe there is something like it for php also. http://serverfault.com/questions/220046/why-is-setting-nginx-as-a-reverse-proxy-a-good-idea – northben Mar 30 '15 at 14:53

1 Answers1

2

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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Is it defined by developpers ? Or does the PHP script automatically detect if it is static content ? – mimipc Feb 15 '13 at 08:20
  • 1
    The web application developer adds code to the PHP script to send the appropriate header (if they know what they're doing). – Michael Hampton Feb 15 '13 at 08:22