I've been using Lorempixel.com for placeholder images in the early stages of several of my projects. It's a fantastic concept, but one thing I've noticed about the service is that I will quite frequently have connection problems. While generally not a real problem, it is rather annoying and can occasionally delay the loading of a page.
I decided what I would do to resolve this is creating a caching proxy server and link to that instead in my projects. This way the images will be served from the local cache most of the time resulting in fast loading and no connection problems. If a particular image has not yet been loaded then it'll request it from lorempixel and cache the result.
Setting this up in Apache is fairly simple once you find everything you need. Figuring out the right set of modules and their proper configuration was what took me the most time setting this up. You'll need the following modules enabled in apache to set this up.
Once you have those loaded into apache you need to configure them appropriately. The configuration for these modules can go in either the main server configuration or within a specific VirtualHost.
Setting up the cache module requires selection a cache provider and then configuring that provider. In my cache I chose to use a simple disk cache which stores the cached files in a directory on disk. This directory must be writable by the user apache is configured to run is.
CacheEnable disk / CacheRoot /var/www/example.com/cache/
Lorempixel sets up headers to avoid caching. What I wanted is for the server to cache the files despite these no cache headers. To accomplish this we first tell mod_cache to "ignore" requests with no last modified date. Normally if there's no Last-modified header then mod_cache will not cache the response. Ignoring the missing Last-modified header causes it to cache the document anyway.
CacheIgnoreNoLastMod On CacheDefaultExpire 86400
Secondly we need to remove Lorempixel's cache prevention headers so that mod_cache won't see them. We do this using the Header directive to unset the Expires, Cache-control and Pragma headers.
Header unset Expires Header unset Cache-Control Header unset Pragma
Configuring the Proxy
Configuring the proxy is simple. You just turn on the proxy then configure where to forward requests to. We also configure reverse proxy which allows the proxy to re-write and redirect URL's to use the local domain name rather than pass-through the remote domain name.
ProxyRequests On ProxyPass / http://lorempixel.com/ ProxyPassReverse / http://lorempixel.com/
So that's pretty much all there is to it. After changing all my links to point to my local cache domain everything was working great. The initial request would go through to lorempixel and grab an image. That image would get cached on my server for a day and all future requests that day for that image would be served up locally without delay.
Here's the full virtual host block configuration I'm using so you can see everything together as an email.
<VirtualHost *:80> ServerName example.com #Cache CacheEnable disk / CacheRoot /var/www/example.com/cache/ CacheIgnoreNoLastMod On CacheDefaultExpire 86400 Header unset Expires Header unset Cache-Control Header unset Pragma #Proxy ProxyRequests On ProxyPass / http://lorempixel.com/ ProxyPassReverse / http://lorempixel.com/ </VirtualHost>