Page 102 of 169

Phusion Passenger and environment variables with Ruby (Rake) applications

Sometimes we get software where all (or most of) settings are passed into application server via environment variables. But Passenger is not executed directly. So how do we pass ENV variables into it?

We need to create a little "wrapper" around Ruby, so it will be executed with our environment variables. Here's a little example (ruby_wrapper.sh):

#!/bin/sh
export  MONGODB_HOST=172.20.20.45 \
export  MONGODB_PORT=27017 \
export MONGODB_DBNAME=openaudit \
export REDIS_HOST=172.20.20.45 \
export REDIS_PORT=6379 \
export REDIS_DB=10 \

exec "/usr/local/bin/ruby" "$@"

Script must be executable with chmod +x and should be placed in /usr/local/bin/ dir.

There is one more thing to do: tell Passenger where the wrapper is (vhost example):

    <VirtualHost *:3001>
      ServerName 127.0.0.1
      RackEnv production
      PassengerMaxPoolSize 30
      PassengerUseGlobalQueue on
      PassengerRuby /usr/local/bin/ruby-wrapper
      DocumentRoot /home/smhtng/app/current/public/
      PassengerUser openaudit   
      <Directory /home/smhtng/app/current/public/>
         AllowOverride all             
         Options -MultiViews            
      </Directory>
   </VirtualHost>

The "magic" happens here:

 PassengerRuby /usr/local/bin/ruby-wrapper

Rails 3.1 – Sending Content-Length header using middleware – Rack::ContentLength

Some of applications need to send content-length header in every request. For example, when sending something to a mobile device - it would be nice to tell this device how much data are we sending, so the mobile user could see real progress bar.

How to do it in Ruby on Rails 3.1? Well, here you have a list of rack middleware. There is one called Rack::ContentLength, which we will be using. To do so, just include following line into application.rb:

    config.middleware.use Rack::ContentLength

There is a lot interesting stuff going on in rack middleware. For example you can also see this Railscast for more details.

Here example of full response header containing content-length:

Date	Sun, 04 Sep 2011 12:46:46 GMT
Server	Apache/2.2.14 (Debian)
X-Powered-By	Phusion Passenger (mod_rails/mod_rack) 2.2.11
Etag	"a464ed7f54b5637d393232d9dbd40523"
X-UA-Compatible	IE=Edge,chrome=1
X-Rack-Cache	miss
X-Runtime	0.039739
Cache-Control	must-revalidate, private, max-age=0
Content-Length	888
Status	200
Keep-Alive	timeout=15, max=100
Connection	Keep-Alive
Content-Type	application/json; charset=utf-8

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑