NGINX Snippets
Revision as of 01:54, 8 March 2019 by imported>Jeremy-busk
Most of these involve lua
Add to location
Return 200 with text
add_header Content-Type text/plain; return 200 "hi";
default_type 'text/plain'; content_by_lua_block { ngx.req.read_body() local request_body = ngx.req.get_body_data() ngx.say(request_body) ngx.say('hello') ngx.say(ngx.var.arg_id) if string.find(ngx.var.request_body, "eth") then ngx.say('contains') end if ngx.re.match(ngx.var.request_body, "feth") then ngx.say('contains2') end
Loging vars in lua to error log
ngx.log(ngx.ERR, 'mytest or var');
p = 666 d = "23.42" payload='{"d":[{"pres":'..(p)..',"temp":"'..(d)..'"}]}' t = cjson.decode(payload) print(t.d[1].temp) -- prints "23.42"
Getting Headers
local h = ngx.req.get_headers() for k, v in pairs(h) do ngx.say(k, ": ", v) end
local redis = require "nginx.redis" local rdb = redis:new() rdb:set_timeout(1000) -- 1 sec local ok, err = rdb:connect("127.0.0.1", 6379) if not ok then ngx.say("failed to connect: ", err) return end if res ~= ngx.var.http_authorization then ngx.say("Failed http authorization.") return end ngx.encode_base64("myuser:pass") if res ~= ngx.var.http_authorization then ngx.say("Failed http authorization.") return end