Category Archives: Rails

Rewriting URL params in nginx

I came across this problem recently, a customer was moving to Ruby on Rails from another framework/language (.NET I think) and needed to re-write a bunch of URLs. Some needed the query parameters rewriting too. One example was rewriting the old search path, so the old URL:

would become:

This should be fairly simple except for the qry parameter needed to be changed to query. A bit of googling didn’t turn up much but with some experimentation I came up with this using the pre-populated nginx $args variable:

location /OldSearchPath.aspx {
  if ($args ~* qry=(.+)) {
    set $args query=$1;
rewrite ^.+$ /search redirect;

It even leaves the other parameters intact, so the pagination will still work.

Tagged ,

NWRUG February 2009 – Nanite talk

For the first time in quite a while we had a talk at NWRUG, it seemed to go well and the free Pizzas and Beer provided by Engine Yard were very popular. About 12 people turned up. I was the only speaker and did a 45 minute talk on Nanite with a brief introduction to cloud-computing as that’s the environment I see Nanite being most useful.

Thanks to everyone who turned up and Engine Yard for the sponsorship. I promised a blog post with links to some of the resources from the talk, and here it is!

Useful links from the talk

Nanite (of course)

Kestrel (a starling replacement)

Delayed Job

Warren (A wrapper around AMQP from brightbox)

Engine Yard Solo

As I mentioned in the talk you can probably get away with using third-party APIs and calling it ‘cloud-computing’, this set of slides is really interesting:

Web Hooks and the Programmable World of Tomorrow

Lastly the slides on SlideShare, though they don’t make as much sense as they do with the talk & my notes.


Pastie: control rabbitMQ using Nanite, controlling god using Nanite.

Next Month

More talks! Asa Calow has agreed to do a talk on Solr and I rather foolishley agreed to do another talk on Sphinx.

Tagged , , , , ,

My First Rails Plugin – asset-format

For the first time ever today I wrote a rails plugin! I googled around first to make sure no-one had solved the particular problem I needed fixing but no-one seemed to have done, so I had no choice but to jump into the rails core…

The problem

In RubyonRails using the asset link tag such as:

<%= stylesheet_link_tag "screen", :media=>"screen, projection" %>

results in something like this on your HTML:

<link href="/stylesheets/screen.css?1234174480" media="screen, projection" rel="stylesheet" type="text/css" />

(view source on the Recycling Group Finder for the original). The number after the question mark (‘?1234174480’) is the asset ID. The idea behind the asset ID is that you can configure your web-server to set an Expires headers for your static assets far into the future causing browsers to cache the asset, but by changing the number (Rails does this automatically) you indicate to the browser that a file has changed and that the browser should re-fetch it. It works most of the time, but there are some mis-configured proxies out there (and possibly browsers too) that will cache the asset even when the asset id changes.

There’s a good description of the problem over on the sproutcore blog.

The Solution

This is where my plugin comes in, altering the asset URL with the asset ID in the URL, eg:




Placing the asset ID in the URL, not the query causes proxies and browsers to see a different asset and so to fetch what it sees as a completely new asset. It will then cache the asset and if you change the asset ID the same thing will happen, no more over-cached assets!

Installing it

Easy, just:

$ cd rails_project_base_dir && script/plugin install git://

start up your app and your asset URLs will be magically transformed! You will need a rewrite in your web-server config to rewrite the request back to the asset, so:

/stylesheets/1234174480/screen.css -> /stylesheets/screen.css

an you are all set!

Whoooooosh – the Recycling Group Finder gets faster

This afternoon I implemented a CDN (Content Distribution Network)  for the Recycling Group Finder, it only took about an hour and has slashed page load times on the site.

Whilst working for Engine Yard today (yes, on a Saturday!) we got a ticket in asking for CDN recommendations. This got me thinking about the recently announced Amazon Cloudfront CDN, specifically how cool it would be to use it for the Recycling Group Finder (a personal project). It’s an internationally targeted site and would benefit from the faster page load times a CDN will bring.

Why Cloudfront?

Cloudfront is built on Amazon S3, a well-used and trusted technology and many of Engine Yard’s customers are already using it. The chances are if you’re already using S3 for storing assets Cloudfront will be dead-simple to get running. I wasn’t, though it didn’t take much extra effort.

It’s easy…

Really easy. There’s a really useful tutorial up that gets you most of the way there, the Firefox S3 plugin is amazing. All that was needed after that was a single line change to my environment.rb file in my RubyonRails project:

and my javascript and CSS files were being served super-fast from the Cloudfront CDN. Sweet! It took a bit more work (and a custom helper) to get some of the other assets served, but not much.

Very. Very. Fast.

The speedup is significant. The site seems snappier when visiting in a browser but I also ran some before and after speed tests using the Pingdom tools page. The results were impressive. The homepage went from a 16.1 second load time (including all assets) to 1.1 seconds:

Before (16.1 seconds):

Homepage for Recycling Group Finder served from server

After (1.1 seconds):

Homepage for Recycling Group Finder served from Cloudfront

The homepage saw the most impressive speed boost, but sub-page load times improved significantly too. This is the before and after tests for the San Francisco Freecycle page:

Before (5.6 seconds):

Recycling Group Finder sub-page served by the server

After (1.6 seconds):

Recycling Group Finder sub-page served by the Cloudfront CDN

Worth the effort

Hell yeah! It took a really short amount of time to get running (helped by RubyonRails built in support for asset hosts) and the site flies. Give it a go!


Bot whipping

I finally got round to doing something about the Alexa crawler tampering with the cookies on the Recycling Group finder after Patrick Joyce commented on my previous post.

There’s no reason for me to store information in the cookies for bots visiting my site so I just disabled them for the Alexa crawler (any request where the user agent string matches ia_archiver) by adding a single line to my ApplicationController class:

It wasn’t a major pain in the arse, but it’s a few less emails to delete every day!

Next NW Ruby User Group: 20th November

20th November – Pub meet: RubyConf aftermath

This month the meeting will be two weeks after RubyConf so we will all be getting together to discuss any exciting news that may have found it’s way out onto the blogs. Maybe drink some curry and beer too!


7:00pm :: Social meeting in The Paramount

Sign Up

If you would like to attend this event, please sign up on the Upcoming event page

Monthly meetups

This month also marks the start of regular monthly meetups. These will be the third Thursday of the month, guaranteed, if no speakers are available the meeting will be social.


Flushing memcached servers the easy way

This is easy right?  Can’t you just restart the memcached server? Well yes, but you may cause errors in applications that are already connected to it. You can follow your memcached restart with an application restart, eg for a Ruby on Rails app:

# /etc/init.d/memcached restart && mongrel_rails cluster::restart

Of course if you have more than one application server you have to restart your app on every single one. This would work on an engineyard slice assuming you have the eycap gem installed:

$ cap production memcached:restart
$ cap production mongrel:restart

Restarting your application is not ideal however, you will lose anything cached in memory, cause delays to users trying to access your site, that sort of thing.

So what can be done? The answer is really simple. Assuming a memcached running on the local machine on the default port:

$ echo “flush_all” | nc localhost 11211



It is my pleasure to announce the Recycling group finder, Something I have been working on for the past couple of weeks with my wonderful employer 29degrees. For those of you who don’t know, Freecycle is a worldwide recycling network, in their own words:

The Freecycle Network™ is made up of 4,205 groups with 4,211,000 members across the globe. It’s a grassroots and entirely nonprofit movement of people who are giving (& getting) stuff for free in their own towns. It’s all about reuse and keeping good stuff out of landfills. Each local group is moderated by a local volunteer (them’s good people).

As the name suggests, Freecycle Group Finder is a new way to find Freecycle groups.

Finding a group – the old way

To find a group you enter a location to search for in the box on the Freecycle homepage. I live in Romiley, so I enter that and click search. But it can’t find any groups! Failed freecycle search

In order for it to find my local group I would have to guess it was called ‘Stockport‘ and search for that. I could have used the Freecycle group browser but who browses anymore? People demand search! I wanted to make this better so I wrote the Recycling Group Finder.

Finding a group – the way

Just enter any location into the search box on the homepage and it will find all your local groups: Successful search

The Freecycle search gets it right sometimes. Take a search for Alameda, CA. It lists all the groups nearby, but Freecycle Group Finder does better. Freecycle Group Finder shows you a map of where all the local groups are! It lets you scroll around and visually determine the closest group (Try it for yourself!). An improvement we feel, and one that will help more people join up and start recycling.

What keeps it rolling?

At 29degrees we’re big fans of Ruby on Rails. It helps us make web applications faster, and with more fun, and it was no exception for the Freecycle Group finder. We are using Postgres for the database and serving it all with mongrel and of course Apache. Of course if wouldn’t be anwhere near as good if it wasn’t for Tony, 29degrees co-founder and designer extroadinaire!

Hi I’m Ruby on Rails

Another funny Ruby on Rails/PHP video:

Hi I’m Ruby on Rails

A couple of funny videos in the style of the new Apple adverts:

Rails and Java

Rails and PHP