Directives

mruby_nginx_module's directives are following.

mruby_cache

  • Syntax : mruby_cache on OR off
  • Default: on
  • Context: main, server, location, location if

This directive sets whether mruby_cache is enabled or disabled. When mruby_cache is enabled the byte-code of mruby is compiled only once in start-up. But It does not mean that This directive is alwaly applied. In the directives that uses inline-code, mruby_cache is always enabled. (For example, mruby_content_handler_code)

mruby_require

  • Syntax : mruby_require #{mruby_script_path}
  • Default: on
  • Context: http
  • Phase: loading config

This directive loads mruby script of #{mruby_script_path} in loading configuration.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_init_code

  • Syntax : mruby_init_code #{mruby_script_string}
  • Context: http
  • Phase: loading config

mruby_init_code receives and executes a string of mruby-script in loading config.

mruby_init_code "@var = 'mruby'";

server {
    listen      8000;
    server_name localhost;
    location /print_var {
        mruby_content_handler_code "
            Nginx.rputs(@var)
            Nginx.rputs('\n')
        ";
    }
}
$ curl http://localhost:8000/print_var
mruby
$

mruby_init

  • Syntax : mruby_init #{mruby_script_path}
  • Context: http
  • Phase: loading config

mruby_init is equal to mruby_init_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_set_code

  • Syntax : mruby_set_code #{mruby_script_string} #{arg1} #{arg2} ...
  • Context: server, server if, location, location if
  • Phase: server-rewrite, rewrite
server {
    listen      8000;
    server_name localhost;
    location /set {
        mruby_set_code $bokko "'cubic' + 'daiya'";
        echo $bokko;
    }
}
$ curl http://localhost:8000/set
cubicdaiya
$

This directive requires ngx_devel_kit.

mruby_set

  • Syntax : mruby_set #{mruby_script_path} #{arg1} #{arg2} ...
  • Context: server, server if, location, location if
  • Phase: server-rewrite, rewrite

mruby_set is equal to mruby_set_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

This directive requires ngx_devel_kit.

mruby_rewrite_handler_code

  • Syntax : mruby_rewrite_handler_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: rewrite

mruby_rewrite_handler

  • Syntax : mruby_rewrite_handler #{mruby_script_path}
  • Context: http, server, location, location if
  • Phase: rewrite

mruby_rewrite_handler is equal to mruby_rewrite_handler_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_access_handler_code

  • Syntax : mruby_access_handler_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: access

mruby_access_handler

  • Syntax : mruby_access_handler #{mruby_script_path}
  • Context: http, server, location, location if
  • Phase: access

mruby_access_handler is equal to mruby_access_handler_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_content_handler_code

  • Syntax : mruby_content_handler_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: content

mruby_content_handler

  • Syntax : mruby_content_handler #{mruby_script_path}
  • Context: http, server, location, location if
  • Phase: content

mruby_content_handler is equal to mruby_content_handler_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_log_handler_code

  • Syntax : mruby_log_handler_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: log

mruby_log_handler

  • Syntax : mruby_log_handler #{mruby_script_path}
  • Context: http, server, location, location if
  • Phase: log

mruby_log_handler is equal to mruby_log_handler_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_header_filter_code

  • Syntax : mruby_header_filter_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: output-header-filter
server {
    listen       8000;
    server_name  localhost;
    root html;

    location / {
        mruby_header_filter_code  "
            r = Nginx::Request.new()
            r.content_type = 'text/plain'
        ";
    }
}

The response from nginx is following.

$ curl -I "http://localhost:8000/index.html"
HTTP/1.1 200 OK
Server: nginx/1.5.3
Date: Sat, 27 Jul 2013 04:27:27 GMT
Content-Type: text/plain
Content-Length: 627
Last-Modified: Sun, 21 Oct 2012 19:40:18 GMT
Connection: keep-alive
ETag: "50844fa2-264"
Accept-Ranges: bytes

mruby_header_filter

  • Syntax : mruby_body_filter_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: output-header-filter

mruby_header_filter is equal to mruby_header_filter_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.

mruby_body_filter_code

  • Syntax : mruby_body_filter_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: output-body-filter
server {
    listen       8000;
    server_name  localhost;
    root html;

    location / {
        mruby_body_filter_code "
            '<html>\n' + ARGV[0] + '</html>\n'
        ";
    }
}

It assumes that /index.html is following.

<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!!!</h1></center>
</body>

The response from nginx is following.

$ curl "http://localhost:8000/index.html"
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!!!</h1></center>
</body>
</html>
$

mruby_body_filter

  • Syntax : mruby_body_filter_code #{mruby_script_string}
  • Context: http, server, location, location if
  • Phase: output-body-filter

mruby_body_filter is equal to mruby_body_filter_code except its argument is a path of mruby-script.

#{mruby_script_path} is able to be set as absolete or relative path. The root of relative path is the basename of --conf-path in configure.