Question

I have a small dynamic site implemented in mod_python. I inherited this, and while I have successfully made relatively minor changes to its content and logic, with HTTP caching I'm out of my depth. The site works fine already, so this isn't "the usual question" about how to disable caching for a dynamic site.

My problem is there is one large banner image on each page (the same image from same URL on each page) which accounts for ~90% of site bandwidth but which so far as I can tell isn't being cached; as I browse the site every time I click to a new page (or back to a previously visited one) there it is downloading it yet again.

If I wget the banner's image URL (to see the headers) I see:

$ wget -S http://example.com/site/gif?name=banner.gif
--2012-04-04 23:02:38--  http://example.com/site/gif?name=banner.gif
Resolving example.com... 127.0.0.1
Connecting to example.com|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Date: Wed, 04 Apr 2012 22:02:38 GMT
  Server: Apache/2.2.14 (Ubuntu)
  Content-Location: gif.py
  Vary: negotiate
  TCN: choice
  Set-Cookie: <blah blah blah>
  Connection: close
  Content-Type: image/gif
Length: unspecified [image/gif]
Saving to: `gif?name=banner.gif'

and the code which is serving it up isn't much more than

    req.content_type = 'image/gif'
    req.sendfile(fullname)

where fullname is a file-path munged from the request's name parameter.

My question is: is there some quick fix along the lines of setting an Expires: or Vary: field in the image's request response which will result in clients being less keen to repeatedly download it ?

The site is hosted on Ubuntu 10.04 and doesn't have any non-default apache mods enabled other than rewrite.

I note that most (not all) of the site pages' headers themselves do contain

Pragma: no-cache
Cache-Control: no-cache
Expires: -1
Vary: Accept-Encoding

(and the original site author has clearly thought about this as no-cache is applied selectively to non-static content pages). I don't know enough about caching to know whether this somehow poisons the included .gif IMG into being reloaded every time too though.

Was it helpful?

Solution

I don't know if my answer can help you or not, but I post it anyway. Instead of serving image files from within python application, you can create another virtualhost within apache (on same server) just to serve static and image file. In your python application, you can embed image likes this

<img src="http://img.yoursite.com/banner.gif" alt="banner" />

With separate virtualhost, you can add various header to various content type using mode header, or add another caching layer for your static file.

Hope this help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top