HttpLuaModule获取GET和POST参数
Eave
2017.04.18 15:22
GET方式
local id = tostring(ngx.var.arg_id)
local type = tostring(ngx.var.arg_type)
POST方式
ngx.req.read_body()
local args = ngx.req.get_post_args()
local id = tostring(args["id"])
local type = tostring(args["type"])
两种方式混合
local request_method = ngx.var.request_method
local args = nil
if "GET" == request_method then
nginx.say("Get")
args = ngx.req.get_uri_args()
else
nginx.say("Post")
ngx.req.read_body()
args = ngx.req.get_post_args()
end
local id = tostring(args["id"])
local type = tostring(args["type"])
IP地址
local ip_addr = ngx.var.remote_addr
当前时间
os.date("%Y-%m-%d %H:%M:%S")
os.time()
响应
-- 设置响应头信息
ngx.header["Content-Type"] = "application/json;charset=UTF-8"
-- 获取Lua版本号
ngx.header["X-Powered-By"] = _VERSION
ngx.status = ngx.HTTP_OK
-- 内容输出
ngx.say(cjson.encode(resp))
-- 设置响应状态码
ngx.exit(ngx.HTTP_OK)
HTTP状态码
100 ngx.HTTP_CONTINUE
101 ngx.HTTP_SWITCHING_PROTOCOLS
200 ngx.HTTP_OK
201 ngx.HTTP_CREATED
202 ngx.HTTP_ACCEPTED
204 ngx.HTTP_NO_CONTENT
206 ngx.HTTP_PARTIAL_CONTENT
300 ngx.HTTP_SPECIAL_RESPONSE
301 ngx.HTTP_MOVED_PERMANENTLY
302 ngx.HTTP_MOVED_TEMPORARILY
303 ngx.HTTP_SEE_OTHER
304 ngx.HTTP_NOT_MODIFIED
307 ngx.HTTP_TEMPORARY_REDIRECT
308 ngx.HTTP_PERMANENT_REDIRECT
400 ngx.HTTP_BAD_REQUEST
401 ngx.HTTP_UNAUTHORIZED
402 ngx.HTTP_PAYMENT_REQUIRED
403 ngx.HTTP_FORBIDDEN
404 ngx.HTTP_NOT_FOUND
405 ngx.HTTP_NOT_ALLOWED
406 ngx.HTTP_NOT_ACCEPTABLE
408 ngx.HTTP_REQUEST_TIMEOUT
409 ngx.HTTP_CONFLICT
410 ngx.HTTP_GONE
426 ngx.HTTP_UPGRADE_REQUIRED
429 ngx.HTTP_TOO_MANY_REQUESTS
444 ngx.HTTP_CLOSE
451 ngx.HTTP_ILLEGAL
500 ngx.HTTP_INTERNAL_SERVER_ERROR
501 ngx.HTTP_NOT_IMPLEMENTED
501 ngx.HTTP_METHOD_NOT_IMPLEMENTED
502 ngx.HTTP_BAD_GATEWAY
503 ngx.HTTP_SERVICE_UNAVAILABLE
504 ngx.HTTP_GATEWAY_TIMEOUT
505 ngx.HTTP_VERSION_NOT_SUPPORTED
507 ngx.HTTP_INSUFFICIENT_STORAGE