Caching web files

I have a web server running on a Mega w/Ethernet shield and I am having problems caching my pages.

The arduino is on a private network and provides feedback and configuration of a couple of machines. Because of this, I see no reason for the arduino to be repeatedly sending pages since they will never change. The data will change, but that is all handled using ajax calls, but the html, js, css and images will not change.

So I am trying to force the browser to cache everything. To do that I am using the following headers to send a page to the browser (this one is for the css file for example)....

HTTP/1.1 200 OK Cache-Control: public,max-age=120 Content-Type: text/css Connection: keep-alive

After sending the headers, I send the file which resides on the SD card.

As I understand it, by using that header, if I refresh the page within 120 seconds, the browser should be using the cached copy of the css file rather than requesting it, but that is not the case. Each refresh is including a request for the css which the arduino gladly servers up.

I realize that this may not be a arduino question per se` but I am hopping someone can tell me what am I doing wrong, and how can I cache my pages. Thanks for your time. kevin

if I remember correctly HTTP/1.1 requires origin servers to send a Date header, if possible, with every response, giving the time at which the response was generated - that's quite important for cache expiration (needs to be sent as NTP time)

if you don't provide it, there are heuristic expiration time calculation but HTTP/1.1 specification impose worst-case constraints on their results.

So I'd be tempted to get the date from a NTP server, and stick the date into the header.

not tested though

Thanks for the reply!

As it turned out, I had it right all along. The problem was how I was loading the page in the browser. Apparently Chrome will only use cached pages if the page is loaded from a book mark. Loading the page by pressing enter from the address bar, or by pressing F5 will force a server update.

props for the help J-L-M kevin