A Rate-Limiting HTTP Proxy(2)LUA Nginx Req JSON

299 篇文章 0 订阅
A Rate-Limiting HTTP Proxy(2)LUA Nginx Req JSON

Go Through the Samples and Learn
https://github.com/362228416/openresty-web-dev

Reload the nginx if needed.
>nginx -p /Users/carl/work/openresty -c conf/nginx.conf -s reload

LUA IDE
https://eclipse.org/ldt/#installation

Change the nginx.conf to as follow:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
lua_code_cache off;
location ~ /lua/(.+) {
default_type text/html;
content_by_lua_file lua/$1.lua;
}
}
}

Visit this URL will mapping to hello.lua
http://localhost:8080/lua/hello

http://localhost:8080/lua/welcome will visit welcome.lua

Nginx will get the HEADER and GET information, Nginx usually will not get the POST information.

GET
local args = ngx.req.get_uri_args(); — table including all the get params
local id = nix.var.arg_id — get single param

POST
ngx.req.read_body() — fetch the POST body
local args = nix.req.get_post_args() — get the table including all POST params

METHOD of HTTP
local request_method = ngx.var.request_method ———— GET or POST

LUA language
https://www.lua.org/pil/contents.html

Provide a LUA package to handle the request params
req.lua
local _M = {}

-- fetch both http get/post params
function _M.getArgs()
local request_method = ngx.var.request_method
local args = ngx.req.get_uri_args()
-- POST args
if "POST" == request_method then
ngx.req.read_body()
local postArgs = ngx.req.get_post_args()
if postArgs then
for k, v in pairs(postArgs) do
args[k] = v
end
end
end
return args
end

Need to load all the packages in NGINX
http {
lua_package_path /opt/luaweb/lua/?.lua;
server {
listen 8080;
lua_code_cache off;
location ~ /lua/(.+) {
default_type text/html;
content_by_lua_file lua/$1.lua;
}
}
}

Use that module in LUA script
local req = require "req"

local args = req.getArgs()

local name = args['name']

if name == nil or name == "" then
name = "Guest"
end

ngx.say("<p>hello, " .. name .." Welcome to LUA</p>")

Visit and Give parameters
http://localhost:8080/lua/hello?name=Sillycat

This is easy and working as well
lua_package_path "$prefix/lua/?.lua”;

How to Handle JSON
It seems that is a LUA module called cjson

local req = require "req"
local cjson = require "cjson"

local args = req.getArgs()

-- convert string to JSON object
local json_str = '{"name": "Carl.Luo", "age": 35}'
local json = cjson.decode(json_str)

local json_str2 = cjson.encode(json);
ngx.say(json_str2 .. "<br />");

local name = args['name']

if name == nil or name == "" then
name = "Guest"
end

ngx.say("Name = " .. json['name'] .. ", Age = " .. tostring(json['age']) .. "\n")

ngx.say("<p>hello, " .. name .." Welcome to LUA</p>")


References:
http://idevz.github.io/vanilla/
https://github.com/362228416/openresty-web-dev/tree/master/demo1
https://github.com/bungle/awesome-resty
https://github.com/362228416/openresty-web-dev/tree/master/demo3

OAUTH2
https://github.com/pingidentity/lua-resty-openidc
https://github.com/hypebeast/micro-auth

Previous Blog
http://sillycat.iteye.com/blog/2374164
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值