Monday, May 20, 2013

Running a rails server to support SSL

Ok, so here's the problem. Rails' development server doesn't ship with the option to accept https requests, which is quite annoying when developing an application that demands it.

That is why I'm using the 'Thin' server instead of the default 'WEBrick' server.
Simply installed like this:

$ gem install thin

The next step is to generate the certificates (self-signed):
$ openssl req -new -newkey rsa:2048 -sha1 -days 365 -nodes -x509 -keyout .ssl/server.key -out .ssl/server.crt

Since these certificates should only be used in development, I added them to the .gitignore file.

Now, we just need to run Thin with the certificates. However, the server still cannot serve both http and https requests on the same port, so we create 2 separate servers, one for the non-SSL and one for the SSL requests:

$ thin start -p 3000
$ thin start -p 3001 --ssl --ssl-verify --ssl-key-file .ssl/server.key --ssl-cert-file .ssl/server.crt

Checkout this blog entry by Railway for more details, and a code snippet for letting the application handle the port switching: http://www.railway.at/2013/02/12/using-ssl-in-your-local-rails-environment/