魏长东

weichangdong

东邪

lua学习-1

我的环境用的是 win的openresty。(文章内容来自 http://jinnianshilongnian.iteye.com/blog/)

HelloWorld 的代码

首先配置nginx

location /hello {
            default_type 'text/html';  
	    lua_code_cache off;
	    content_by_lua 'ngx.say("hello world")';
        }

	location /test1 {
            default_type 'text/html';  
	    lua_code_cache off;
	content_by_lua_file  html/test1.lua;
        }
	location ~ /test2/(\d+)/(\d+) { 
	lua_code_cache off;
	    #设置nginx变量  
	    set $a $1;   
	    set $b $host;  
	    default_type "text/html";  
	    #nginx内容处理  
	    content_by_lua_file html/test2.lua;  
	    #内容体处理完成后调用  
	    echo_after_body "ngx.var.b $b";  
	} 

代码

test1.lua文件

ngx.print("im ngx print\n<br />"); 
ngx.say('im ngx say');

test2.lua文件

--nginx变量  
local var = ngx.var  
ngx.say("ngx.var.a : ", var.a, "<br/>")  
ngx.say("ngx.var.b : ", var.b, "<br/>")  
ngx.say("ngx.var[1] : ", var[1], "<br/>")  
ngx.say("ngx.var[2] : ", var[2], "<br/>")  
ngx.var.b = 'it\'s so  wonderfull!!!!!!!!!!!!!!!!';  
  
ngx.say("<br/>")  

  
--请求头  
local headers = ngx.req.get_headers()  
ngx.say("headers begin", "<br/>")  
ngx.say("Host : ", headers["Host"], "<br/>")  
ngx.say("user-agent : ", headers["user-agent"], "<br/>")  
ngx.say("user-agent : ", headers.user_agent, "<br/>")  
ngx.print("output header ================================<br/>")
for k,v in pairs(headers) do  
    if type(v) == "table" then  
        ngx.say(k, " : ", table.concat(v, ","), "<br/>")  
    else  
        ngx.say(k, " : ", v, "<br/>")  
    end  
end  
ngx.say("headers end", "==================<br/>")  
ngx.say("<br/>")  
  
--get请求uri参数  
ngx.say("uri args begin", "<br/>")  
local uri_args = ngx.req.get_uri_args()  
for k, v in pairs(uri_args) do  
    if type(v) == "table" then  
        ngx.say(k, " : ", table.concat(v, ", "), "<br/>")  
    else  
        ngx.say(k, ": ", v, "<br/>")  
    end  
end  
ngx.say("uri args end", "<br/>")  
ngx.say("<br/>")  
  
--post请求参数  
ngx.req.read_body()  
ngx.say("post args begin", "<br/>")  
local post_args = ngx.req.get_post_args()  
for k, v in pairs(post_args) do  
    if type(v) == "table" then  
        ngx.say(k, " : ", table.concat(v, ", "), "<br/>")  
    else  
        ngx.say(k, ": ", v, "<br/>")  
    end  
end  
ngx.say("post args end", "<br/>")  
ngx.say("<br/>")  
  
--请求的http协议版本  
ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "<br/>")  
--请求方法  
ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "<br/>")  
--原始的请求头内容  
ngx.say("ngx.req.raw_header : ",  ngx.req.raw_header(), "<br/>")  
--请求的body内容体  
ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "<br/>")  
ngx.say("<br/>")  

ngx.print('-------------');

test2的访问地址 http://localhost/test2/12/34?wcd=1222222222222222&YY[]=1212&YY[]=3434

输出结果

ngx.var.a : 12
ngx.var.b : localhost
ngx.var[1] : 12
ngx.var[2] : 34

headers begin
Host : localhost
user-agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.11466.7 Safari/537.36
user-agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.11466.7 Safari/537.36
output header ================================
host : localhost
accept-language : zh-CN,zh;q=0.8,en;q=0.6
cookie : PHPSESSID=o9136qhogd7curc972e9stvos2; isBrowsChk=true; isshow=true; isShrinkage=false
connection : keep-alive
accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding : gzip, deflate
upgrade-insecure-requests : 1
user-agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.11466.7 Safari/537.36
headers end==================

uri args begin
wcd: 1222222222222222
YY[] : 1212, 3434
uri args end

post args begin
post args end

ngx.req.http_version : 1.1
ngx.req.get_method : GET
ngx.req.raw_header : GET /test2/12/34?wcd=1222222222222222&YY[]=1212&YY[]=3434 HTTP/1.1 Host: localhost Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.11466.7 Safari/537.36 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.8,en;q=0.6 Cookie: PHPSESSID=o9136qhogd7curc972e9stvos2; isBrowsChk=true; isshow=true; isShrinkage=false 
ngx.req.get_body_data() : nil

-------------ngx.var.b it's so wonderfull!!!!!!!!!!!!!!!!

ngx.var : nginx变量,如果要赋值如ngx.var.b = 2,此变量必须提前声明;另外对于nginx location中使用正则捕获的捕获组可以使用ngx.var[捕获组数字]获取;

ngx.req.get_headers:获取请求头,默认只获取前100,如果想要获取所以可以调用ngx.req.get_headers(0);获取带中划线的请求头时请使用如headers.user_agent这种方式;如果一个请求头有多个值,则返回的是table;

ngx.req.get_uri_args:获取url请求参数,其用法和get_headers类似;

ngx.req.get_post_args:获取post请求内容体,其用法和get_headers类似,但是必须提前调用ngx.req.read_body()来读取body体(也可以选择在nginx配置文件使用lua_need_request_body on;开启读取body体,但是官方不推荐);

ngx.req.raw_header:未解析的请求头字符串;

ngx.req.get_body_data:为解析的请求body体内容字符串。

 

如上方法处理一般的请求基本够用了。另外在读取post内容体时根据实际情况设置client_body_buffer_sizeclient_max_body_size来保证内容在内存而不是在文件中。