How to control Unicorn’s middleware stack

Say your client app consumes only not chunked content (”Transfer-Encoding: chunked” header should not be present) and it requires Content-Length header strictly. How could you achieve that?

Firstly you should set RACK_ENV variable to ‘none’ whether in your deploy.rb file (if you use Capistrano for running Unicorn on staging/production) or just run unicorn_rails prepending with RACK_ENV=none. Here’s the line for the Capistrano’s deploy.rb

set :rack_env, :none 

By specifying that Unicorn’s Rack::Builder will use only your app’s middlewares and wouldn’t add Rack::Chunked and Rack::ContentLength ( please see https://github.com/defunkt/unicorn/blob/master/lib/unicorn.rb#L60)

Ok, so how to add Content-Length header to server’s response then?

That’s pretty simple: just add 

config.middleware.use Rack::ContentLength

to config/environments/environment_file.rb

That’s it! Thanks!


Posted

in

by

Tags:

Comments

One response to “How to control Unicorn’s middleware stack”

  1. A WordPress Commenter Avatar

    Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a Reply

Your email address will not be published. Required fields are marked *