Ruby support

Starting from version 0.9.7-dev a Ruby (Rack/Rails) plugin is officially available. The official modifier number for Ruby apps is 7, so remember to set it in your web server configuration.

The plugin can be embedded in the uWSGI core or built as a dynamically loaded plugin.

Some uWSGI standard features still aren’t supported by the plugin, such as:

See the Ruby API support page for a list of features currently supported.

Building uWSGI for Ruby support

You can find rack.ini in the buildconf directory. This configuration will build uWSGI with a Ruby interpreter embedded. To build uWSGI with this configuration, you’ll need the Ruby headers/development package.

  1. python uwsgiconfig.py --build rack

The resulting uWSGI binary can run Ruby apps.

A rackp.ini build configuration also exists; this will build uWSGI with Ruby support as a plugin; in this case remember to invoke uWSGI with the plugins=rack option.

A note regarding memory consumption

By default the memory management of this plugin is very aggressive (as Ruby can easily devour memory like it was going out of fashion). The Ruby garbage collector is invoked after every request by default. This may hurt your performance if your app creates lots of objects on every request. You can tune the frequency of the collection with the OptionRubyGcFreq option. As usual, there is no one-value-fits-all setting for this, so experiment a bit.

If your app leaks memory without control, consider limiting the number of requests a worker can manage before being restarted with the max-requests option. Using limit-as can help too.

A note regarding threads and fibers

Adding threading support in Ruby 1.8 is out of discussion. Thread support in this versions is practically useless in a server like uWSGI.Ruby 1.9 has a threading mode very similar to the Python one, its support is available starting from uWSGI 1.9.14 using the “rbthreads” plugin.

Fibers are a new feature of Ruby 1.9. They are an implementation of coroutines/green threads/stop resume/cooperative multithreading, or whatever you’d like to call this class of funny technologies. See FiberLoop.

Running Rack applications on uWSGI

This example shows you how to run a Sinatra application on uWSGI.

config.ru

  1. require 'rubygems'
  2. require 'sinatra'
  3.  
  4. get '/hi' do
  5. "Hello World!"
  6. end
  7.  
  8. run Sinatra::Application

Then invoke uWSGI (with —plugins if you built Ruby support as a plugin):

  1. ./uwsgi -s :3031 -M -p 4 -m --post-buffering 4096 --rack config.ru
  2. ./uwsgi --plugins rack -s :3031 -M -p 4 -m --post-buffering 4096 --rack config.ru

Note

post-buffering is required by the Rack specification.

Note

As Sinatra has a built-in logging system, you may wish to disable uWSGI’s logging of requests with the disable-logging option.

Running Ruby on Rails applications on uWSGI

As writing formal documentation isn’t very interesting, here’s a couple of examples of Rails apps on uWSGI.

Running Typo

  1. sudo gem install typo
  2. typo install /tmp/mytypo
  3. ./uwsgi -s :3031 --lazy-apps --master --processes 4 --memory-report --rails /tmp/mytypo --post-buffering 4096 --env RAILS_ENV=production

–lazy-apps is vital here as typo (like a lot of apps) is not fork-friendly (it does not expect is loaded in the master and then fork() is called). With this optionthe app is fully loaded one-time per-worker.

Nginx configuration:

  1. location / {
  2. root "/tmp/mytypo/public";
  3. include "uwsgi_params";
  4. uwsgi_modifier1 7;
  5. if (!-f $request_filename) {
  6. uwsgi_pass 127.0.0.1:3031;
  7. }
  8. }

Running Radiant

  1. sudo gem install radiant
  2. radiant /tmp/myradiant
  3. cd /tmp/myradiant
  4. # (edit config/database.yml to fit)
  5. rake production db:bootstrap
  6. ./uwsgi -s :3031 -M -p 2 -m --rails /tmp/myradiant --post-buffering 4096 --env RAILS_ENV=production

Apache configuration (with static paths mapped directly):

  1. DocumentRoot /tmp/myradiant/public
  2.  
  3. <Directory /tmp/myradiant/public>
  4. Allow from all
  5. </Directory>
  6.  
  7. <Location />
  8. uWSGISocket 127.0.0.1:3032
  9. SetHandler uwsgi-handler
  10. uWSGIForceScriptName /
  11. uWSGImodifier1 7
  12. </Location>
  13.  
  14. <Location /images>
  15. SetHandler default-handler
  16. </Location>
  17.  
  18. <Location /stylesheets>
  19. SetHandler default-handler
  20. </Location>
  21.  
  22. <Location /javascripts>
  23. SetHandler default-handler
  24. </Location>

Rails and SSL

You may wish to use the HTTPS / UWSGI_SCHEME https uwsgi protocol parameters to inform the app that it is running under HTTPS.

For Nginx:

  1. uwsgi_param HTTPS on; # Rails 2.x apps
  2. uwsgi_param UWSGI_SCHEME https; # Rails 3.x apps