Web-server – How to make HTTP Cache Headers Work

apache-2.2cachehttpweb-server

Can someone please help me with a HTTP caching question?

I thought that if I set the "Expires" and "Cache-Control:max-age" request headers, then a browser wouldn't make a GET request until after the max-age had passed?

My server is seeing lots of conditional GET requests, and it replies with a 304. However, I'd like to eliminate these conditional GETs if possible and have the browser only ask for a resource once it thinks it has expired.

Here's the details I get from the developer tools in Chrome:

Request URL:http://localhost:8080/img/branding.gif
Request Method:GET
Status Code:304 Not Modified

Request Headers:

Cache-Control:max-age=0
If-Modified-Since:Thu, 22 Jul 2010 10:54:34 GMT
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4

Response Headers:

Cache-Control:max-age=2246400, s-maxage=0
Connection:Keep-Alive
Content-Location:/img/branding.gif
Content-Type:image/gif
Date:Fri, 23 Jul 2010 15:06:10 GMT
Expires:Fri, 07 Jan 2011 15:06:10 GMT
Keep-Alive:timeout=15, max=100
Last-Modified:Thu, 22 Jul 2010 10:54:34 GMT
Pragma:cache
Server:Oracle Application Server Containers for J2EE 10g (9.0.4.0.0)

I set the cache headers in a filter in my Java code. The code is running on Oracle's OC4J.

Also, I want to run this over HTTPS. Will using the same headers work for HTTPS too?

Thanks in advance.

Best Answer

if you remove the Last-Modified and ETag header, you will totally eliminate If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses, so a file will stay cached without checking for updates until the Expires header indicates new content is available.

source: http://www.askapache.com/htaccess/apache-speed-last-modified.html