April 22, 2015

Nginx as a Refile caching image proxy

Using Nginx as a Refile caching proxy for images with Ruby on Rails.

For many of our recent Rails projects we have switched to using Refile to serve and process images. There has been a short learning curve, but it makes our workflow much easier to be able to resize and process on demand. One of the issues we ran into is that for some of our environments (staging etc.) we only use Nginx, there is no caching CDN like AWS CloudFront. This means that many image requests hit the Rails app directly, which can cause significant slowdown in page load time. To fix this we wanted Nginx to cache and serve image once processed, so the only the first request hits Rails. Handily Nginx has a proxy_cache directive that you can use to do this, it took a little tweaking to figure it out, so hopefully the following configuration helps someone out there save some time: