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
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