Embed mruby into Nginx

mruby_nginx_module is the powerful extension by mruby for nginx.


The concept of mruby_nginx_module is following.

  • Give developers to develop nginx module by mruby
  • Usability that higher affinity with Nginx


location /mruby {
    mruby_content_handler_code "
        sum = 0
        (1..10).each { |i| sum += i }
        Nginx.rputs(sum.to_s + "\n") #=> 55


Minumum Require - nginx - you shoukd be able that have all library dependecy to build nginx from source - mruby - ruby require

Optional - mgem - mruby low level gems - ngx_devel_kit - nginx-echo-module


Minumum Require


$ git clone https://github.com/cubicdaiya/mruby_nginx_module.git
$ cd mruby_nginx_module
$ git submodule update --init
$ cd mruby
$ # Optional commands if you want
$ rake ENABLE_GEMS="true" CFLAGS="-O2 -fPIC"


$ # download and unpack stable nginx version
$ cd nginx-1.4.1 # for eaxmple
$ ./configure --add-module=../mruby_nginx_module
$ make
$ sudo make install

Optional commands


$ gem install mgem
$ mgem update && mgem list 
$ mgem add mruby-userdata # for example
$ mgem active mruby-userdata
$ mgem config # answer the question and then you should add this 
$ # replcae build_config.rb with last text output or make changes by hand, then
$ rake ENABLE_GEMS="true" CFLAGS="-O2 -fPIC" 


$ # download and unpack stable nginx version
$ cd nginx-1.4.4 # for eaxmple
$ ./configure --with-pcre-jit --add-module=../nginx-echo-module \
--add-module=../mruby_nginx_module --add-module=../ngx_devel_kit
$ make
$ sudo make install

If you want to use mruby_set and mruby_set_code, you may embed ngx_devel_kit when executing configure with --add-module.

Difference from ngx_mruby

mruby_nginx_module is forked from ngx_mruby at July 2013.

Though I used to develop ngx_mruby with ngx_mruby's developers, what I want to develop were gradually departured from what they want to develop.

What I want to develop are as previously noted concepts.

  • Give developers by mruby to develop nginx module
  • Usability that higher affinity with Nginx
  • More features

ngx_mruby is a part of unified web server extension developing support institution(like mod_mruby).

But what I want to develop is the product that is more specialized for Nginx.

In detail, mruby_nginx_modules has the following features currently.

  • Regexp(built-in regular expression engine)
  • Nginx::Context(hash table for sharing data in each request processing phase)
  • Nginx::Time(Nginx Time API binding)
  • Nginx::Digest(Nginx Digest API binding)
  • Nginx::Base64(Nginx Base64(en|de)coding API binding)
  • mruby_require(The directive for requiring external mruby script)

Additionally I'm going to implement the following features.

  • Nginx::Shared(Nginx shared memory API biding)
  • Nginx::Subrequest(Nginx sub-request API biding)
  • Nginx::Socket(Nginx non-blocking socket API biding)

Source Code


Table of contents