Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the best way of caching images on my website?

I have a website

www.somesite1.com which gets all its image content from www.somesite2.com

At the moment each time an image is to be displayed we simply use an absolute URL to get it like this

<img src="http://www.somesite2.com/images/myimage.jpg" />

So each time a user goes to www.somesite1.com for content www.somesite2.com gets hammered.

I'm looking for clever ways of caching images, perhaps using http headers or such like. Thanks

like image 935
m3clov3n Avatar asked Dec 31 '25 22:12

m3clov3n


2 Answers

The simplest answer is to set an HTTP Expires header with a far future value (say 10 years). This way (in theory) the browser should never request that image again taking huge load off your server if you have a lot of repeat visits. Of course, you'll need to come up with a mechanism for changing the image url when the you want to update the image. Most people approach this by adding a build number into the path somewhere (or by appending a build number to a query string).

There's some good information on performance (including use of the Expires header) here:

http://developer.yahoo.com/performance/rules.html

I personally wouldn't worry about caching these on the server side (I presume they aren't being generated dynamically). They are being served directly from disc so the overhead should be small anyway. Make sure you have a separate machine for serving static content and use a very lightweight web server, such as Lighttpd or nginx which should help increase throughput.

http://www.lighttpd.net/

http://nginx.net/

like image 174
Andy Hume Avatar answered Jan 02 '26 13:01

Andy Hume


Unless your website is very popular you probably don't need a second server for static content.

If you are using Apache HTTP then you could install mod_cache. You could also look at installing Squid Cache.

I agree with Andy Hume though, if you do require a second server then keep it light and use something like lighttpd and Squid.

like image 40
Steve Claridge Avatar answered Jan 02 '26 13:01

Steve Claridge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!