Skip to content
Ryo Okubo edited this page Aug 25, 2016 · 8 revisions

Use Case

  • There are some examples on example/ directory.
  • You may replace some ATS plugins.

Hello world

  • unified_hello.rb
if server_name == "NGINX"
  Server = Nginx
elsif server_name == "Apache"
  Server = Apache
elsif server_name == "ApacheTrafficServer"
  Server = ATS
end

Server::rputs "Hello #{Server::module_name}/#{Server::module_version} world!"
# mod_mruby => "Hello mod_mruby/0.9.3 world!"
# ngx_mruby => "Hello ngx_mruby/0.0.1 world!"
# ts_mruby  => "Hello ts_mruby/0.0.1 world!"
  • remap.config
map /test http://127.0.0.1/ @plugin=ts_mruby.so @pparam=etc/trafficserver/unified_hello.rb

Load balancing

  • Randomize a target host of ATS origin server.
backends = [
  "127.0.0.1:8001",
  "127.0.0.1:8002",
  "127.0.0.1:8003",
]
upstream = ATS::Upstream.new
upstream.server = backends[rand(backends.length)]

Header rewriting (An alternative of Header Rewrite Plugin)

r = ATS::Request.new
conn = ATS::Connection.new
r.headers_in["X-ATS-Plugin"] = "ts_mruby"
r.headers_in["X-Forwarded-For"] = conn.remote_ip
r.headers_in.delete("Cookie")

Regexp base reverse proxy rule (An alternative of Regex Remap Plugin)

req = ATS::Request.new
path = req.uri + req.args

if path =~ /^\/(ogre.*)\/bad/
  ATS::return ATS::HTTP_NOT_FOUND
elsif path =~ /^\/oldurl\/(.*)$/
  ATS::redirect 'http://news.example.com/new/' + $1, ATS::HTTP_MOVED_TEMPORARILY
end

Flexible overriding configuratrions (An alternative of Configuration Remap Plugin)

  • Enable cache:
records = ATS::Records.new
records.set ATS::Records::TS_CONFIG_HTTP_CACHE_HTTP, 1

Change to maintenance mode with Redis

redis = Redis.new '127.0.0.1', 6789

if redis['ts_maint']
  body = <<'EOS'
<html>
  <head>
    <title>Message from your proxy ...</title>
  </head>
  <body>
    <p>Sorry, maintainance mode now ...</p>
  </body>
</html>
EOS
  ATS::echo body
end

Markdown Web Page

discount = Discount.new("http://kevinburke.bitbucket.org/markdowncss/markdown.css", "Markdown Web page example")
md = <<'EOS'
# Markdown Web page example

* You can write web pages by Markdown format.
* The doc will converted to html when HTTP requests come.

## Requirements

* [mruby-discount](https://github.com/matsumoto-r/mruby-discount)

## Example

* This .rb file is it!
EOS

html = discount.header
html << discount.md2html(md)
html << discount.footer

ATS::rputs html

Image resizer

See https://github.com/syucream/ts_mruby/tree/master/examples/resizer