Use Case
- Hello World
- Reverse Proxy
- Change to maintenance mode with Redis
- Markdown Web Page
- Output Filter Converting Markdown to HTML
- File Based Access List
- File Server like Dropbox Link
- Dynamic Image Provider (in Japanese)
- Dynamic reverse proxy using access information (in Japanese)
- Research ngx_mruby and use output filter (in Russian)
- ACME auto ssl using Let’s Encrypt
Hello world
Section titled “Hello world”/path/to/hello.rb
Section titled “/path/to/hello.rb”Nginx.echo "Hello World"nginx.conf
Section titled “nginx.conf”server { location /hello { mruby_content_handler /path/to/hello.rb; }}Access
Section titled “Access”$ curl http://127.0.0.1/helloHello WorldReverse Proxy
Section titled “Reverse Proxy”/path/to/proxy.rb
Section titled “/path/to/proxy.rb”backends = [ "127.0.0.1:8001", "127.0.0.1:8002", "127.0.0.1:8003",]# write algorithm for selecting backendbackends[rand(backends.length)]nginx.conf
Section titled “nginx.conf”server { location /proxy { mruby_set $backend /path/to/proxy.rb; proxy_pass http://$backend; }}Change to maintenance mode with Redis
Section titled “Change to maintenance mode with Redis”refs: http://takeswim.hateblo.jp/entry/2013/10/03/182748
This example needs the Redis class of mruby-redis, and a running Redis server. The default build_config.rb builds mruby-redis again since 2.7.2; 2.7.1 left it out.
nginx.conf
Section titled “nginx.conf”worker_processes 1;
daemon off;master_process off;
events { worker_connections 1024;}
http { # Create redis connection object at startup, # and set it into user-data object mruby_init_code ' userdata = Userdata.new "redis_data_key" userdata.redis = Redis.new "127.0.0.1", 6379 ';
server { listen 7777; location / { echo "port 7777 contents"; } }
server { listen 57777;
location /mruby { mruby_set_code $maint ' userdata = Userdata.new "redis_data_key" redis = userdata.redis redis.get "ngx_maint" '; if ($maint = "TRUE") { mruby_set_code $res_maint ' JSON::stringify({"result"=>"ERR", "time"=>Time.now.to_i}) '; echo $res_maint; } if ($maint != "TRUE") { proxy_pass http://127.0.0.1:7777; } } }}Markdown Web Page
Section titled “Markdown Web Page”Activate mruby-discount when build ngx_mruby
Section titled “Activate mruby-discount when build ngx_mruby” # use markdown on mod_mruby conf.gem :git => 'git://github.com/matsumotory/mruby-discount.git'nginx.conf
Section titled “nginx.conf”location ~ \.rb$ { mruby_add_handler on;}r = Nginx::Request.newr.content_type = "text/html"
# setup markdown enginetitle = "md test"css = "https://gist.github.com/andyferra/2554919/raw/2e66cabdafe1c9a7f354aa2ebf5bc38265e638e5/github.css"md = Discount.new css, title
# create markdown databody = <<DATA
# Section## aaa
- hoge- foo
## bbb
__code__
a = 1 b = a + 1
DATA
# create htmlhtml = md.headerhtml << body.to_htmlhtml << md.footer
# create responseNginx.echo htmlResponse (images)
Section titled “Response (images)”Access to http://example.com/md.rb

Output Filter Converting Markdown to HTML
Section titled “Output Filter Converting Markdown to HTML”.md markdown file convert to html using output filter
Activate mruby-discount when build ngx_mruby
Section titled “Activate mruby-discount when build ngx_mruby” # use markdown on ngx_mruby conf.gem :git => 'git://github.com/matsumotory/mruby-discount.git'nginx.conf
Section titled “nginx.conf”location ~ \.md$ { mruby_output_filter /path/to/filter.rb cache;}/path/to/filter.rb
Section titled “/path/to/filter.rb”f = Nginx::Filter.new
# setup markdown convert enginecss = "https://gist.github.com/andyferra/2554919/raw/2e66cabdafe1c9a7f354aa2ebf5bc38265e638e5/github.css"title = "markdown"md = Discount.new css, title
# convert markdown to html,# and add output filterf.body = md.md2html f.bodytest.md
Section titled “test.md”# Section## aaa
- hoge- foo
## bbb
__code__
a = 1 b = a + 1Access markdown file
Section titled “Access markdown file”http://example.com/test.md image

File Based Access List
Section titled “File Based Access List”nginx.conf
Section titled “nginx.conf”location / { mruby_access_handler /path/to/access_check.rb;}/path/to/access_check.rb
Section titled “/path/to/access_check.rb”r = Nginx::Request.new
filename = File.join r.var.document_root, ".access_list"if File.exists? filename deny_list = Array.new File.open(filename) do |file| while line = file.gets deny_list << line.chomp end end if deny_list.include? r.var.remote_addr Nginx.errlogger Nginx::LOG_ERR, "ACL: FORBIDDEN Client Matched in #{filename}" Nginx.return Nginx::HTTP_FORBIDDEN else Nginx.errlogger Nginx::LOG_ERR, "ACL: OK Client Unmatched in #{filename}" Nginx.return Nginx::HTTP_OK endelse Nginx.return Nginx::HTTP_OKend/path/to/.access_list
Section titled “/path/to/.access_list”192.168.12.9192.168.12.10Access to nginx(run with 192.168.12.9 listening 0.0.0.0:80)
Section titled “Access to nginx(run with 192.168.12.9 listening 0.0.0.0:80)”$ curl http://127.0.0.1/hello world
$ curl http://192.168.12.9/<html><head><title>403 Forbidden</title></head><body bgcolor="white"><center><h1>403 Forbidden</h1></center><hr><center>nginx/1.4.4</center></body></html>error.log
Section titled “error.log”2014/03/10 17:14:20 [error] 11376#0: *4 ACL: OK Client Unmatched in /usr/local/nginx-1.4.4/html/.access_list, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "127.0.0.1"
2014/03/10 17:15:09 [error] 11376#0: *7 ACL: FORBIDDEN Client Matched in /usr/local/nginx-1.4.4/html/.access_list, client: 192.168.12.9, server: localhost, request: "GET / HTTP/1.1", host: "192.168.12.9"File Server like Dropbox Link
Section titled “File Server like Dropbox Link”This example needs the Redis class of mruby-redis, and a running Redis server. The default build_config.rb builds mruby-redis again since 2.7.2; 2.7.1 left it out.
nginx.conf
Section titled “nginx.conf”location /dropbox { mruby_rewrite_handler /path/to/dropbox.rb;}/path/to/dropbox.rb
Section titled “/path/to/dropbox.rb”redis = Redis.new "127.0.0.1", 6379reqst = Nginx::Request.new
file = redis.hget reqst.uri, "file"expr = redis.hget reqst.uri, "expire"
Nginx.errlogger Nginx::LOG_ERR, "dropbox: uri=#{reqst.uri} file=#{file} expire=#{Time.at expr.to_i}"
redis.close
if expr.to_i >= Time.now.to_i Nginx.redirect fileelse Nginx.return Nginx::DECLINEDendInput data by redis-cli
Section titled “Input data by redis-cli”HSET /dropbox/aaabbbcccddd file /files/hoge.htmlHSET /dropbox/aaabbbcccddd expire 1400000000/files/hoge.html
Section titled “/files/hoge.html”hogeaccess to http://127.0.0.1/dropbox/aaabbbcccddd
Section titled “access to http://127.0.0.1/dropbox/aaabbbcccddd”$ curl http://127.0.0.1/dropbox/aaabbbcccdddhogeerror.log
Section titled “error.log”2014/03/11 00:02:30 [error] 12245#0: *109 dropbox: uri=/dropbox/aaabbbcccddd file=/files/hoge.html expire=Wed May 14 01:53:20 2014, client: 127.0.0.1, server: localhost, request: "GET /dropbox/aaabbbcccddd HTTP/1.1", host: "127.0.0.1"