worker_processes  1;
events {
    worker_connections  200;
}

daemon off;
master_process off;
error_log   logs/error.log  debug;

http {
    include       mime.types;
    server {
        listen       80;
        server_name  localhost;

        # hello world and cache option
        # mruby_*_handler /path/to/file.rb [cache];
        # # http://localhost/mruby
        location /mruby {
            mruby_content_handler /usr/local/nginx/html/unified_hello.rb cache;
        }

        # hello world example
        location /hello {
          mruby_content_handler_code '
            if server_name == "NGINX"
              Server = Nginx
            elsif server_name == "Apache"
              Server = Apache
            end
             
            r = Server::Request.new
            s = Server::Server.new
            c = Server::Connection.new
            Server.echo "hostname: " + r.hostname
            Server.echo "path: " + s.path
            Server.echo "hello world"
            Server.echo "documento_root: #{s.document_root}"
            Server.echo "path: #{s.path}"
            Server.echo "remote ip: #{c.remote_ip}"
            Server.echo "remote port: #{c.remote_port}"
            Server.echo "user_agent: #{r.headers_in.user_agent}"
            Server.echo "local ip: #{c.local_ip}"
            Server.echo "local port: #{c.local_port}"
             
            r.headers_in.all.keys.each do |k|
              Server.echo "#{k}: #{r.headers_in[k]}"
            end
            if /Mac/ =~ r.headers_in.user_agent
              Server.echo "your pc is mac"
            end
          ';
        }

        # proxy on rewrite phase
        location /proxy {
          mruby_set_code $backend '
            backends = [
              "test1.example.com",
              "test2.example.com",
              "test3.example.com",
            ]
            backends[rand(backends.length)]
          ';
          proxy_pass  http://$backend;
        }

        # include mruby-json
        # http://localhost/
        location / {
            mruby_content_handler_code '
                r = Nginx::Request.new
                r.content_type = "text/html"
                Nginx.rputs "hello ngx_mruby world!<br>"
                Nginx.rputs "content_type: #{r.content_type}<br><br>"
                Nginx.rputs "ngx_mruby_version: #{Nginx.module_version}<br>"
                Nginx.rputs "nginx_version: #{Nginx.nginx_version}<br>"
                Nginx.errlogger Nginx::LOG_ERR, "ngx_mruby error!"
            ';
        }

        # headers
        # http://localhost/headers
        location /headers {
            mruby_content_handler_code '
                r = Nginx::Request.new
                r.content_type = "text/html"
                Nginx.rputs "headers: #{r.headers_in.all}<br><br>"
            ';
        }

        # vars
        # http://localhost/vars?version=hello
        location /vars {
            set $foo "mruby";
            mruby_content_handler_code '
                r = Nginx::Request.new
                r.content_type = "text/html"
                Nginx.rputs "version => #{r.var.arg_version}<br>"
                Nginx.rputs "host => #{r.var.host}<br>"
                Nginx.rputs "foo => #{r.var.foo}<br>"
                Nginx.rputs "uri => #{r.var.uri}<br>"
                Nginx.rputs "user_agent => #{r.var.http_user_agent}<br>"
            ';
        }

        # redirect
        # http://localhost/redirect?url=http://www.google.com
        location /redirect {
            mruby_rewrite_handler_code '
                r = Nginx::Request.new
                if r.var.arg_url
                    Nginx.redirect r.var.arg_url
                else
                    Nginx.redirect "http://www.yahoo.co.jp", 301
                end
            ';
        }

        # internal redirect
        # http://localhost/redirect/internal?version=xxx
        location /redirect/internal {
            mruby_rewrite_handler_code '
                Nginx.redirect "/vars"
            ';
        }

        # dynamic the internal path for your business
        # http://localhost/redirect/internal/dynamic/path?path=hello
        # actually this request will served by location /static/
        location /redirect/internal/dynamic/path {
            mruby_rewrite_handler_code '
                r = Nginx::Request.new
                dynamic_path = "/static/#{r.var.arg_path}"
                Nginx.redirect dynamic_path
            ';
        }

        # control nginx internal varable between mruby and nginx

        location /inter_var_file {
            set $fuga "200";
            mruby_set $hoge "/usr/local/nginx/html/set.rb";
            mruby_content_handler "/usr/local/nginx/html/set2.rb";
        }

        location /inter_var_inline {
            set $fuga "100";
            mruby_set_code $hoge 'Nginx::Var.new.fuga.to_i * 2';
            mruby_content_handler_code '
              r = Nginx::Request.new
              Nginx.rputs "fuga => #{r.var.fuga} "
              Nginx.rputs "hoge => #{r.var.hoge} "
              r.var.set "hoge", r.var.hoge.to_i * 2
              Nginx.rputs "hoge => #{r.var.hoge} "
            ';
        }

        location ~ \.rb$ {
            mruby_add_handler on;
        }

        location /static/ {
            alias /path/to/static/;
        }

    }
}
