nginx-lua get post请求小例子,nginx-luaget


nginx 启动,停止,重启命令(电脑为ubuntu环境),参考:http://wenku.baidu.com/link?url=B7X7OUwxb9Gp29kUfPO3EHDt9rksUQ_ltNTB95Yq9NZSTlLl-dEZjMe3ZdtC0FTVK7H_NGe1vGkOxSMdm2je4O8G3VqXiW7TATriKVkQB6S

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 方法,其实就是写在/usr/local/nginx/conf/nginx.conf文件中

Nginx安装及配置文件: http://www.cszhi.com/20120513/nginx_nginx-conf.html

安装Nginx+Lua+OpenResty开发环境配置全过程实例

http://www.cnblogs.com/zdz8207/p/Nginx-Lua-OpenResty.html

http {
    include       mime.types;
    default_type  application/octet-stream;

    #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
    #lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块
    #lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块



nginx_test.conf(nginx_test.conf 把location定义好)

如下的location /test/post_2 即使一个指定路径 , 并且指定了处理请求的文件是 hello_http.lua

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 文件内容如下,主要是接收到post请求的body后,对post参数进行遍历输出

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

相关内容

    暂无相关文章