nginx-lua get post请求小例子

nginx 启动,停止,重启命令(电脑为ubuntu环境),参考:http://blog.csdn.net/aidenliu/article/details/6413342

sudo /usr/local/nginx/sbin/nginx

sudo /usr/local/nginx/sbin/nginx -s stop

sudo /usr/local/nginx/sbin/nginx -s reload


/usr/local/nginx/conf/nginx.conf 修改配置文件, 加上自己的url

这里采用了include 方法


nginx_test.conf

location /test/post_1{
	content_by_lua '
		ngx.req.read_body()
		local args = ngx.req.get_post_args()
		for key, val in pairs(args) do
			if type(val) == "table" then
				ngx.say(key, ": ", table.concat(val, ", "))
			else
				ngx.say(key, ": ", val)
			end
		end
	';
}
location /test/post_2{
	 lua_need_request_body on ;
         default_type 'text/plain' ;
 	 content_by_lua_file /home/ding/data/luafile/hello_http.lua ;
}

hello.http.lua

local request_method = ngx.var.request_method
local args = nil

ngx.say('处理htpp请求,get或post请求的参数如下')

if "GET" == request_method then
    ngx.say("get请求")
    args = ngx.req.get_uri_args()
elseif "POST" == request_method then
    ngx.say("post请求")
    ngx.req.read_body()
    args = ngx.req.get_post_args()
end
for key, val in pairs(args) do
	if type(val) == "table" then
		ngx.say(key, ": ", table.concat(val, ", "))
	else
		ngx.say(key, ": ", val)
	end
end
ngx.say('get or post request over')


测试结果


参考学习

nginx-lua :  http://outofmemory.cn/code-snippet/14396/nginx-and-lua

curl请求:http://blog.sina.com.cn/s/blog_6e2d53050101k230.html


你可能感兴趣的:(nginx-lua get post请求小例子)