├── 10 └── nginx.conf ├── 11 └── nginx.conf ├── 01 └── nginx.conf ├── 02 └── nginx.conf ├── 03 └── nginx.conf ├── 04 └── nginx.conf ├── 05 └── nginx.conf ├── 06 └── nginx.conf ├── 06a └── nginx.conf ├── 07 └── nginx.conf ├── 07a └── nginx.conf ├── 08 └── nginx.conf ├── 08a └── nginx.conf ├── 09 └── nginx.conf └── README.md /01/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location /sum { 13 | mruby_set_code $sum ' 14 | result = 0 15 | for i in 0..Nginx::Var.new.arg_n.to_i do 16 | result += i 17 | end 18 | result 19 | '; 20 | return 200 $sum; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /02/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | Nginx.echo "Hello" 15 | '; 16 | mruby_log_handler_code ' 17 | if Nginx::Var.new.status == Nginx::HTTP_OK 18 | Nginx.errlogger(Nginx::LOG_INFO, "Success") 19 | else 20 | Nginx.errlogger(Nginx::LOG_ERR, "Error") 21 | end 22 | '; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /03/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location /internal_redirect { 13 | mruby_content_handler_code ' 14 | Nginx.redirect "/internal" 15 | '; 16 | } 17 | 18 | location /internal { 19 | return 200 "internal redirection"; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /04/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location /image/ { 13 | mruby_rewrite_handler_code ' 14 | file = Nginx::Var.new.uri.match(/^\/image\/(.+\.jpg)$/) 15 | if !file 16 | return Nginx::HTTP_FORBIDDEN 17 | end 18 | url = "/image/jpg/" + file[1] 19 | Nginx.redirect url 20 | '; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /05/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | set $data1 "foo1"; 14 | mruby_set_code $data2 "bar1"; 15 | mruby_content_handler_code ' 16 | v = Nginx::Var.new 17 | v.data1 = "foo2" 18 | v.data2 = "bar2" 19 | v.data3 = "buzz2" 20 | Nginx.rputs "#{v.data1}, #{v.data2}, #{v.data3}" 21 | '; 22 | 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /06/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | args = Hash[*Nginx::Request.new.args.split("&").map{|arg| arg.split("=")}.flatten] 15 | args.each do |k, v| 16 | Nginx.echo "#{k}:#{v}" 17 | end 18 | '; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /06a/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | args = Nginx::Request.new.get_uri_args 15 | args.each do |k, v| 16 | Nginx.echo "#{k}:#{v}" 17 | end 18 | '; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /07/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | args = {"pass" => "ngx_lua"} 15 | args = args.map{|k,v| "#{k}=#{v}"}.join("&") 16 | r = Nginx::Request.new 17 | r.args = args 18 | 19 | Nginx.echo(r.args) 20 | '; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /07a/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | args = {"pass" => "ngx_lua"} 15 | r = Nginx::Request.new 16 | r.set_uri_args(args) 17 | 18 | Nginx.echo(r.args) 19 | '; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /08/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | r = Nginx::Request.new 15 | args = Nginx::Request.new.body 16 | 17 | if !args 18 | Nginx.echo "failed to get post args." 19 | return 20 | end 21 | 22 | Hash[*args.split("&").map{|arg| arg.split("=")}.flatten] 23 | args.each do |k, v| 24 | Nginx.echo "#{k}:#{v}" 25 | end 26 | '; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /08a/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_content_handler_code ' 14 | r = Nginx::Request.new 15 | args = r.get_post_args 16 | 17 | if !args 18 | Nginx.echo "failed to get post args." 19 | return 20 | end 21 | 22 | args.each do |k, v| 23 | Nginx.echo "#{k}:#{v}" 24 | end 25 | '; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /09/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | server { 10 | listen 3000; 11 | 12 | location / { 13 | mruby_rewrite_handler_code ' 14 | Userdata.new.phases = ["rewrite"] 15 | Nginx.return Nginx::DECLINED 16 | '; 17 | 18 | mruby_access_handler_code ' 19 | Userdata.new.phases << "access" 20 | Nginx.return Nginx::DECLINED 21 | '; 22 | 23 | mruby_content_handler_code ' 24 | Userdata.new.phases << "content" 25 | 26 | Userdata.new.phases.each do |phase| 27 | Nginx.echo phase 28 | end 29 | '; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /10/nginx.conf: -------------------------------------------------------------------------------- 1 | worker_processes 1; 2 | daemon off; 3 | error_log /dev/stdout info; 4 | 5 | events {} 6 | 7 | http { 8 | access_log /dev/stdout; 9 | 10 | mruby_init_code ' 11 | c = Cache.new :namespace => "writing" 12 | c["publisher"] = "技術評論社" 13 | c["book"] = "nginx実践入門" 14 | '; 15 | 16 | server { 17 | listen 3000; 18 | 19 | location / { 20 | mruby_content_handler_code ' 21 | c = Cache.new :namespace => "writing" 22 | Nginx.echo "出版社: #{c["publisher"]}" 23 | Nginx.echo "書籍名: #{c["book"]}" 24 | '; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /11/nginx.conf: -------------------------------------------------------------------------------- 1 | # ex. `curl http://localhost:3000/?expires=1483110000&signature=BgUv73Idig2tdwo+WHFwxSdwlBI=&publickey=nginx` 2 | worker_processes 1; 3 | daemon off; 4 | error_log /dev/stdout info; 5 | 6 | events {} 7 | 8 | http { 9 | access_log /dev/stdout; 10 | 11 | server { 12 | listen 3000; 13 | 14 | set $publickey "nginx"; 15 | set $privatekey "mruby"; 16 | 17 | location / { 18 | mruby_content_handler_code ' 19 | arg_publickey = Nginx::Var.new.arg_publickey 20 | arg_signature = Nginx::Var.new.arg_signature 21 | arg_expires = Nginx::Var.new.arg_expires 22 | 23 | expires = arg_expires.to_i 24 | 25 | publickey = Nginx::Var.new.publickey 26 | privatekey = Nginx::Var.new.privatekey 27 | 28 | if arg_publickey && arg_publickey.match(publickey) || !arg_expires 29 | Nginx.return Nginx::HTTP_FORBIDDEN 30 | end 31 | 32 | plaintext = "#{Nginx::Var.new.request_method}#{Nginx::Var.new.uri}#{arg_expires}#{publickey}" 33 | hmac_sha1 = Digest::HMAC.digest(plaintext, privatekey, Digest::SHA1) 34 | signature = Base64::encode(hmac_sha1) 35 | 36 | if expires < Time.now.to_i 37 | Nginx.return Nginx::HTTP_FORBIDDEN 38 | end 39 | 40 | if signature == arg_signature 41 | Nginx.echo "Sucess" 42 | else 43 | Nginx.return Nginx::HTTP_FORBIDDEN 44 | end 45 | '; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-tech-talk 2 | 3 | nginx 実践入門 9 章の "Lua による nginx の拡張" を ngx_mruby を用いて実現したサンプルコード集です。 4 | --------------------------------------------------------------------------------