Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome or Safari browser. Firefox 10 (to be released soon) will also handle it.

Scripting Nginx with LUA

Hiroshi Miura @miurahr

Nginx

Nginx

nginx.org

OpenResty

OpenResty.org

Nginx Config(1)

  server {
listen 80;
listen [::]:80 default_server;

root /usr/share/nginx/html;
index index.html index.htm;

server_name localhost;

Nginx Config(2)

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.html;
}

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    deny all;
}

Non blocking I/O with Nginx

Use memcached with nginx and module.

  location /memcached {
    set $memc_cmd $arg_cmd;
    set $memc_key $arg_key;
    set $memc_value $arg_val;
    set $memc_exptime $arg_exptime;
    memc_pass 127.0.0.1:11211;
  }

Lua

Lua

lua.org

Lua

Why Lua

Lua Nginx Module

Hello, Lua

location /hellolua {
    default_type 'text/plain';

    content_by_lua '
       local name = ngx.var.arg_name or "Anonymous"
        ngx.say("Hello, ", name, "!")
    ';
}

$ curl http://localhost/hellolua?name=Lua
Hello, Lua! 

Load Lua files

Modules are loaded once, on the first request

location /lua {
    rewrite_by_lua_file /path/to/rewrite.lua;
    access_by_lua_file /path/to/access.lua;
    content_by_lua_file /path/to/content.lua;
}

Lua Nginx API

CoSocket

Non-blocking networking I/O with "Cosocket"

Cosocket example

location /memcached {
    content_by_lua '
        local sock = ngx.socket.connect("127.0.0.1", 11211)
        sock:send("SET foo bar 3600\r\n")
        local line = sock:receive()
        if line then
            ngx.say(line)
        end
        sock:setkeepalive()    ';
}

$ curl http://localhost/memcached
STORED

Synchnonization support

context data

Per-request context data = Lua table to store data duraring request lifetime

 location /ctx {
  access_by_lua '
    ngx.ctx.userid = 'abcde'
  ';
  content_by_lua '
    ngx.say(ngx.ctx.userid)
  ';
 }
 $ curl http://localhost/ctx
abcde

Shared dictionary

Global hash table across all requests

http {
    lua_shared_dict stats 10m;
    server {
        location / {
            content_by_lua '
               ngx.shared.stats:incr("hits", 1)
               ngx.say(ngx.shared.stats:get("hits"))
            ';
        }
    }
}

Lua-resty-* family

lua-resty-memcached

location /memcached {
    content_by_lua '
        local memcached = require "resty.memcached"
        local memc = memcached:new()
        local ok, err = memc:connect("127.0.0.1", 11211)

        local ok, err = memc:set("foo", "bar", 3600)
        if ok then
            ngx.say("STORED")
        end

        memc:set_keepalive()    ';
}

Use Case

Use Case

Build Tile serving service - TileMan

Credit

Questions?

introduction to OpenResty

Use a spacebar or arrow keys to navigate