lua取参数

发布时间:2017-11-24 18:36:11 阅读:1050次

http://blog.csdn.net/qq_26437925/article/details/50946025

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


[cpp] view plain copy
  1. http {  
  2.     include       mime.types;  
  3.     default_type  application/octet-stream;  
  4.   
  5.     #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找  
  6.     #lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块  
  7.     #lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块  


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

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

[plain] view plain copy
  1. location /test/post_1{  
  2.     content_by_lua '  
  3.         ngx.req.read_body()  
  4.         local args = ngx.req.get_post_args()  
  5.         for key, val in pairs(args) do  
  6.             if type(val) == "table" then  
  7.                 ngx.say(key, ": ", table.concat(val, ", "))  
  8.             else  
  9.                 ngx.say(key, ": ", val)  
  10.             end  
  11.         end  
  12.     ';  
  13. }  
  14. location /test/post_2{  
  15.      lua_need_request_body on ;  
  16.          default_type 'text/plain' ;  
  17.      content_by_lua_file /home/ding/data/luafile/hello_http.lua ;  
  18. }  

hello.http.lua 文件内容如下,主要是接收到post请求的body后,对post参数进行遍历输出

[plain] view plain copy
  1. local request_method = ngx.var.request_method  
  2. local args = nil  
  3.   
  4. ngx.say('处理htpp请求,get或post请求的参数如下')  
  5.   
  6. if "GET" == request_method then  
  7.     ngx.say("get请求")  
  8.     args = ngx.req.get_uri_args()  
  9. elseif "POST" == request_method then  
  10.     ngx.say("post请求")  
  11.     ngx.req.read_body()  
  12.     args = ngx.req.get_post_args()  
  13. end  
  14. for key, val in pairs(args) do  
  15.     if type(val) == "table" then  
  16.         ngx.say(key, ": ", table.concat(val, ", "))  
  17.     else  
  18.         ngx.say(key, ": ", val)  
  19.     end  
  20. end  
  21. 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

版权声明:本文为博主原创文章,未经博主允许不得转载。

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:lua取参数 出自老鄢博客 | 欢迎分享