-
Notifications
You must be signed in to change notification settings - Fork 112
/
nginx.conf
168 lines (149 loc) · 5.23 KB
/
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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/;
}
}
}