Advertisement
Guest User

ngx_lua example

a guest
Oct 26th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. user       www www;
  2. worker_processes 2;
  3. error_log  /home/wwwlogs/nginx_error.log crit;
  4. pid           /usr/local/openresty/nginx/logs/nginx.pid;
  5. google_perftools_profiles       /tmp/tcmalloc;
  6. worker_rlimit_nofile 51200;
  7. events {
  8.     use epoll;
  9.     worker_connections 51200;
  10. }
  11. http {
  12.     default_type text/html;
  13.  
  14.     upstream redis {
  15.         server 127.0.0.1:6379;
  16.         keepalive 1024;
  17.     }
  18.  
  19.     server {
  20.         listen  8080;
  21.         root    /home/wwwroot;
  22.         index   index.html;
  23.     }
  24.  
  25.     server {
  26.         listen  80;
  27.         root    /home/wwwroot;
  28.         index   index.html;
  29.  
  30.         location = /proxy {
  31.             proxy_pass http://127.0.0.1:8080/;
  32.         }
  33.  
  34.         location = /redis {
  35.             internal;
  36.             set $redis_key $arg_key;
  37.             redis_pass redis;
  38.         }
  39.  
  40.         location = /redis2 {
  41.             internal;
  42.             set $exptime $arg_exptime;
  43.             set $key $arg_key;
  44.             redis2_query set $key $echo_request_body;
  45.             redis2_query expire $key $exptime;
  46.             redis2_pass redis;
  47.         }
  48.  
  49.         location / {
  50.             default_type 'text/html';
  51.             set $key $uri;
  52.             set $exptime 300;
  53.             content_by_lua '
  54.                -- work like the srcache module
  55.                local resa = ngx.location.capture("/redis",
  56.                    { method = ngx.HTTP_GET,
  57.                      args = { key = ngx.var.key } })
  58.                if resa.status == 200 then
  59.                    ngx.say(resa.body)
  60.                else
  61.                    local resb = ngx.location.capture("/proxy")
  62.                    if resb.status == ngx.HTTP_OK then
  63.                        ngx.say(resb.body)
  64.                        local resc = ngx.location.capture("/redis2",
  65.                            { method = ngx.HTTP_PUT,
  66.                              body = resb.body,
  67.                              args = { key = ngx.var.key, exptime = ngx.var.exptime } })
  68.                    else
  69.                        ngx.exit(ngx.HTTP_NOT_FOUND)
  70.                    end
  71.                end
  72.                -- update the redis cache from the proxy even the keys get hit
  73.                if not resb then
  74.                    local resb = ngx.location.capture("/proxy")
  75.                    if resb.status == ngx.HTTP_OK then
  76.                        local resc = ngx.location.capture("/redis2",
  77.                            { method = ngx.HTTP_PUT,
  78.                              body = resb.body,
  79.                              args = { key = ngx.var.key, exptime = ngx.var.exptime } })
  80.                    else
  81.                        ngx.exit(ngx.HTTP_NOT_FOUND)
  82.                    end
  83.                end
  84.            ';
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement