Thursday, June 14, 2012

My introduction to Passenger, nginx and Sinatra - "Not found"

I spent way too long getting passenger and nginx working with my Sinatra app.  My problem was, that I had set up a middleware class.  Instead of using the "top level" of the file to define the responses, I created a class deriving from Sinatra::Base.  So for example I had this for my webmain.rb:

require 'sinatra/base'

class MyApp < Sinatra::Base
   set :sessions, true
   set :foo, 'bar'

   get '/' do
      'Hello world!'

This worked fine when running the application with the rackup command, but in passenger, it just said "not found".  Not a very helpful error message at all.  There was nothing else on the page (if nginx has an error, it says 'nginx' centred on the page.), so apparently the problem was in sinatra.  This is the file that I had:

require 'rubygems'
require 'sinatra'
require File.join(File.dirname(__FILE__), 'webmain.rb')

run Sinatra::Application

Can you see what's wrong?  I couldn't either.  Apparently no-one else had been dense enough to have this problem on stack overflow or anywhere else I could find with google.  After trying different files and using strace on the nginx and passenger processes, I gave up for the night.

The next evening after getting home from work, I came to a realisation.  I changed the file to this:

require 'rubygems'
require 'sinatra'
root_dir = File.dirname(__FILE__)
require File.join(root_dir, 'webmain.rb')

run MyApp
I needed to actually run the class I created. Success!

Note that the File.join stuff is needed because I'm using ruby 1.9, which uses relative paths for "require"s that aren't in the installed directories.

Versions of the software I'm using:

Passenger: 3.0.12
Sinatra: 1.3.2
ruby: 1.9.3p0

No comments:

Post a Comment