ngx_mruby - A Fast and Memory-Efficient Web Server Extension Mechanism Using Scripting Language mruby for nginx
Clone or download
Permalink
Failed to load latest commit information.
.github Update PULL_REQUEST_TEMPLATE.md Aug 2, 2017
dependence/ngx_devel_kit Merge commit 'fc1efc5fd1cdd78c0ff098bef421bafed1e1c204' into update-ndk Jun 27, 2018
docker Update mruby Jun 25, 2014
docs Fix format considering copy-and-paste Nov 15, 2018
example Fix typo Aug 12, 2018
misc More prepare code formatter and editor for development Nov 15, 2018
mrbgems You aren't yourself today. Jun 9, 2018
mruby Merge commit '1ef1a1b4282c5fea1b7d09a89e1db288edb612ef' into support-iv Nov 3, 2018
src Bump up version to 2.1.3 Nov 15, 2018
test Add test for enable iv of v2 Nov 3, 2018
.clang-format Requires clang-format version 6.0 or later May 22, 2018
.gitignore Add ignore rule for vagrant Nov 15, 2018
.gitmodules Don't use submodule Oct 13, 2015
.travis.yml update test target to 1.15.6 and 1.14.1 Nov 7, 2018
AUTHORS Add @yyamano and @pyama86 as authors Aug 2, 2017
Dockerfile Base Docker Image Update Ubuntu 18.04 Aug 19, 2018
LEGAL Add LEGAL file Jul 26, 2013
MITL Update License information Apr 29, 2014
Makefile.in Simplify Makefile. May 15, 2017
README.md Capitalize nginx Sep 7, 2018
Vagrantfile Add development environment using vagrant Nov 15, 2018
apply-clang-format Add tool which apply clang-format to all code Nov 2, 2015
build.sh Fix typos about 'default' Dec 18, 2017
build_config.rb mruby add `mruby-sleep` gem to the core. Sep 6, 2018
config.in Enable to run mruby code within fiber and add async sleep Jan 21, 2018
configure Fix typo Aug 12, 2018
configure.in Fix typo Aug 12, 2018
nginx_version update test target to 1.15.6 and 1.14.1 Nov 7, 2018
test.sh Oops remove unused option Jun 22, 2018
update-devkit-subtree Add ref support to update-devkit-subtree and update-mruby-subtree. Nov 7, 2018
update-mruby-subtree Add ref support to update-devkit-subtree and update-mruby-subtree. Nov 7, 2018

README.md

ngx_mruby

ngx_mruby: A Fast and Memory-Efficient Nginx Extension Mechanism Scripting with mruby.

Documents

What's ngx_mruby

ngx_mruby is A Fast and Memory-Efficient TCP/UDP Load Balancing and Web Server Extension Mechanism Using Scripting Language mruby for nginx.

  • ngx_mruby is to provide an alternative to lua-nginx-module or mod_mruby of Apache httpd.
  • Unified Ruby Code between Apache(mod_mruby), nginx(ngx_mruby) and other Web server software(plan) for Web server extensions.
  • You can implement nginx modules by Ruby scripts on nginx!
  • You can implement some Web server software extensions by same Ruby code (as possible)
  • Supported nginx main-line and stable-line
  • Benchmark between ngx_mruby and lua-nginx-module
# location /proxy {
#   mruby_set $backend "/path/to/proxy.rb";
#   proxy_pass   http://$backend;
# }

backends = [
  "test1",
  "test2",
  "test3",
]

r = Redis.new "192.168.12.251", 6379
r.get backends[rand(backends.length)]
  • see examples
  • Sample of Unified Ruby Code between Apache(mod_mruby) and nginx(ngx_mruby) for Web server extensions
  • You can implement some Web server software extensions by same Ruby code (as possible)
# Unified Ruby Code between Apache(mod_mruby) and nginx(ngx_mruby)
# for Web server extensions.
#
# Apache httpd.conf by mod_mruby
#
# <Location /mruby>
#     mrubyHandlerMiddle "/path/to/unified_hello.rb"
# </Location>
#
# nginx nginx.conf by ngx_mruby
#
# location /mruby {
#     mruby_content_handler "/path/to/unified_hello.rb";
# }
#

Server = get_server_class

Server::rputs "Hello #{Server::module_name}/#{Server::module_version} world!"
# mod_mruby => "Hello mod_mruby/0.9.3 world!"
# ngx_mruby => "Hello ngx_mruby/0.0.1 world!"

ngx_mruby mod_mruby performance

hello world simple benchmark, see details of blog entry.

Abstract

As the increase of large-scale and complex Web services, not only the development of Web applications is required, but also the implementation of Web server extensions in many cases. Most Web server extensions are mainly implemented in the C language because of fast and memory-efficient behavior, but by writing extensions using a scripting language we can achieve better maintainability and productivity.

However, if the existing methods are primarily intended to enhance not the implementation of Web applications but the implementation of internal processing of the Web server, the problem remains in terms of speed, memory-efficiency and safety.

Therefore, we propose a fast and memory-efficient Web server extension mechanism using a scripting language. We designed an architecture where the server process creates a region in memory to save the state of the interpreter at the server process startup, and multiple scripts share this region to process the scripts quickly when new request are made.

The server process frees the global variables table, the exception flag and the byte-code which cause an increase of memory usage, in order to reduce the memory usage and extend safety by preventing interference between each script because of sharing the region. We implemented a mechanism that can extend the internal processing of nginx easily by Ruby scripts using nginx and the embeddable scripting language mruby. It's called "ngx_mruby".

Contributions

This project exists thanks to all the people who contribute. We also welcome financial contributions in full transparency on our open collective.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

This project is under the MIT License: