Rack::Cache and Lighttpd+FCGI July 15th, 2009

On some of the Openminds shared hosting servers we run a Lighttpd+FCGI stack for Ruby/Rails applications. This week we got a complaint from a user saying they had a problem with the latest Radiant 0.8. Every page he loaded was the same as the first page he opened since the last server restart.

When digging into the problem it was clear that it was a problem with Rack::Cache, which only created 1 meta-store entry and served that from there on. On investigation Rack::Cache::Key.call(@request) always returned http://example.com:80/dispatch.fcgi?. Apparently the last part of the key gets created by scriptname and pathinfo:

..snip..
      parts << @request.script_name
      parts << @request.path_info
..snip..

When looking at the request it seems Lighttpd+FCGI doesn’t fill in the env[‘PATH_INFO’], which results in always having the same cache key, thus the same page.

To solve this problem I’ve created a small Rack middleware that sets PATHINFO from REQUESTURI (Like Passenger does with Nginx)

module ::Rack
  class LighttpdFix
    def initialize(app)
      @app = app
    end     

    def call(env)
      env['PATH_INFO'] = env['REQUEST_URI'].split('?', 2).first
      @app.call(env)
    end     
  end
end

# in production.rb
config.middleware.insert_before ::Rack::Cache, ::Rack::LighttpdFix

# or for Radiant
config.middleware.insert_before ::Radiant::Cache, ::Rack::LighttpdFix

I’ll make a gem out of this tomorrow.I made a gem out of this, you can find it on GitHub and install it using RubyGems.

sudo gem install DefV-rack_lighttpd_fix
tags: , , , , l

7 Responses to “Rack::Cache and Lighttpd+FCGI”

  • over 2 years ago Tassilo said

    Hey, just wanted to let you know that I ran into the same issue on BlueHost, running Apache, so it doesn’t seem to be lighttpd specific as your title suggests…

    Thanks for your fix!

  • over 2 years ago Jason Garber said

    I had the same problem running Radiant 0.8.1 under modfcgid. Your little patch worked perfectly once I figured out the config.middleware.insertbefore had to go after config.middleware.use ::Radiant::Cache (obviously!)

  • over 2 years ago Ondrej said

    Hi, thanks very much for fixing the issue. I am having the same problem on BlueHost/Apache as well. Unfortunately I cannot install the gem through “gem install -s http://gems.github.com defv-racklighttpdfix”. I get: “ERROR: could not find gem defv-racklighttpdfix locally or in a repository”. Is there something I’m missing that I have to do prior intalling the gem? Many thanks

  • over 2 years ago Stephen said

    I had the same trouble: ERROR: could not find gem …

    The workaround is to download the gem and install from the local directory i.e.:

    wget http://github.com/downloads/DefV/racklighttpdfix/racklighttpdfix-0.1.gem

    gem install racklighttpdfix-0.1.gem

  • about 1 year ago Michel Filipe said

    Thanks very much for fixing this! Why the issue doesn’t solved yet?

    I’m using radiant-0.9.1 with rails-2.3.8.

  • about 1 year ago Steffen said

    Same for me… Thanks for the fix!

  • about 1 year ago ChaCha.com said

    That little fix was really great. I had the same problem with my shared hosting with hostgator. I used your little gem to fix the issue.

Sorry, comments are closed for this article.