Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balancing browser caching of images and time expiration of links to avoid bandwidth theft

We host our images on Amazon S3, and serve them through Amazon Cloudfront. We'd like to reduce our bandwidth expenses. We believe that we have quite a bit of bandwidth theft, and want to start using signed URLs to prevent this. However, we have to balance this against the fact that each time we send out a new signed url, it invalidates the browser caches for the image.

Our current plan is to generate a signed url that is valid for two months, then cache it locally and serve it for a month, before generating a new signed URL. This will allow the browser to cache the images for longer. However, this also means that anyone can then leech bandwidth for an image for up to two months.

Is there any way to have the link expire sooner, but have the browser continue to cache that image? I know that the etag and If-Modified-Since for the image do not change when the url is resigned for a new timestamp, but the the browser no longer knows to send those values in the header, because as far as it is concerned, this is a new URL. Is there any way around this problem?

like image 334
Karptonite Avatar asked Jul 16 '10 22:07

Karptonite


People also ask

Are images cached in browser?

A browser or Web cache does exactly that, except with program and website assets. When you visit a website, your browser takes pieces of the page and stores them on your computer's hard drive. Some of the assets your browser will store are: Images - logos, pictures, backgrounds, etc.

How does the browser know if its cached page is the current version on the server?

Browsers will usually get this information through HTTP headers sent with the page. For example, the Last-Modified header tells the browser how old the page is. A browser can send a simple HEAD request to the page to get the last-modified value.

How do I enable browser caching?

First thing first, you can leverage browser caching by modifying the . htaccess file. To do so, you can use an FTP client like FileZilla or the file manager in your hosting control panel. This method might suit you well if you're an advanced user.


1 Answers

Have you measured how much of your bandwidth is being stolen? ie, check how many requests you get for your images that don't have a referrer field set to your own site.

For optimum caching, we recommend caching for 10 years, so 2 months sounds like a little short.

like image 187
bluesmoon Avatar answered Oct 21 '22 15:10

bluesmoon