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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
# Use Case
- [Hello World](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#hello-world)
- [Reverse Proxy](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#reverse-proxy)
- [Change to maintenance mode with Redis](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#change-to-maintenance-mode-with-redis)
- [Markdown Web Page](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#markdown-web-page)
- [Output Filter Converting Markdown to HTML](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#output-filter-convering-markdown-to-html)
- [File Based Access List](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#file-based-access-list)
- [File Server like Dropbox Link](https://github.com/matsumotory/ngx_mruby/tree/master/docs/use_case#file-server-like-dropbox-link)
- [Dynamic Image Providor (in Japanese)](http://techhey.hatenablog.com/entry/2014/04/19/162323)
- [Dynamic reverse proxy using access information (in Japanese)](http://blog.cloudpack.jp/2014/07/08/ngx-mruby-nginx-script/)
- [Research ngx_mruby and use output filter (in Russian)](http://habrahabr.ru/post/225313/)
- [ACME auto ssl using Let's Encrypt](https://github.com/matsumotory/ngx_mruby/blob/master/test/conf/auto-ssl/nginx.conf.client_example)
## Hello world
#### /path/to/hello.rb
```ruby
Nginx.echo "Hello World"
```
#### nginx.conf
```nginx
server {
location /hello {
mruby_content_handler /path/to/hello.rb;
}
}
```
#### Access
```bash
$ curl http://127.0.0.1/hello
Hello World
```
## Reverse Proxy
#### /path/to/proxy.rb
```ruby
backends = [
"127.0.0.1:8001",
"127.0.0.1:8002",
"127.0.0.1:8003",
]
# write algorithm for selecting backend
backends[rand(backends.length)]
```
#### nginx.conf
```nginx
server {
location /proxy {
mruby_set $backend /path/to/proxy.rb;
proxy_pass http://$backend;
}
}
```
## Change to maintenance mode with Redis
refs: http://takeswim.hateblo.jp/entry/2013/10/03/182748
#### nginx.conf
```nginx
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
##### Activate mruby-discount when build ngx_mruby
```ruby
# use markdown on mod_mruby
conf.gem :git => 'git://github.com/matsumotory/mruby-discount.git'
```
##### nginx.conf
```nginx
location ~ \.rb$ {
mruby_add_handler on;
}
```
##### md.rb
```ruby
r = Nginx::Request.new
r.content_type = "text/html"
# setup markdown engine
title = "md test"
css = "https://gist.github.com/andyferra/2554919/raw/2e66cabdafe1c9a7f354aa2ebf5bc38265e638e5/github.css"
md = Discount.new css, title
# create markdown data
body = <<DATA
# Section
## aaa
- hoge
- foo
## bbb
__code__
a = 1
b = a + 1
DATA
# create html
html = md.header
html << body.to_html
html << md.footer
# create reponse
Nginx.echo html
```
##### Response (images)
Access to http://example.com/md.rb
***

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

***
## File Based Access List
##### nginx.conf
```nginx
location / {
mruby_access_handler /path/to/access_check.rb;
}
```
##### /path/to/access_check.rb
```ruby
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
end
else
Nginx.return Nginx::HTTP_OK
end
```
##### /path/to/.access_list
```
192.168.12.9
192.168.12.10
```
##### Access to nginx(run with 192.168.12.9 listening 0.0.0.0:80)
```bash
$ 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
```
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
##### nginx.conf
```nginx
location /dropbox {
mruby_rewrite_handler /path/to/dropbox.rb;
}
```
##### /path/to/dropbox.rb
```ruby
redis = Redis.new "127.0.0.1", 6379
reqst = 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 file
else
Nginx.return Nginx::DECLINED
end
```
##### Input data by redis-cli
```redis
HSET /dropbox/aaabbbcccddd file /files/hoge.html
HSET /dropbox/aaabbbcccddd expire 1400000000
```
##### /files/hoge.html
```html
hoge
```
##### access to http://127.0.0.1/dropbox/aaabbbcccddd
```curl
$ curl http://127.0.0.1/dropbox/aaabbbcccddd
hoge
```
##### 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"
```
|