nginx-openresty-lua使用

以下配置均在/usr/local/openresty/nginx/conf/nginx.conf中配置,涉及到的lua脚本均创建在/usr/local/openresty/nginx/conf

1. localtion下直接写lua脚本:content_by_lua

location /lua {
   default_type text/html; #响应内容为html解析
   content_by_lua 'ngx.say("<br>hello world ! welcome to openresty!</br>")'; # content_by_lua:调用lua脚本
           
}

2. 调用lua脚本文件:content_by_lua_file

 location /lua1 {
     default_type text/html; #响应内容为html解析
     content_by_lua_file conf/001.lua; # content_by_lua_file:调用lua脚本
 }

001.lua脚本内容:

ngx.say("<br>hello world ! welcome to openresty!</br>")

3. 调用lua脚本文件:include

location /lua2 {
    default_type text/html; #响应内容为html解析
    include lua.conf; # include :调用lua脚本
}

 lua.conf脚本:

content_by_lua_file conf/002.lua; # content_by_lua_file:调用lua脚本

 002.lua脚本:

ngx.say("<br>hello world ! welcome to openresty! i am lua2</br>")

4. 调用lua脚本:content_by_lua_block

location /lua3 {
    default_type text/html; #响应内容为html解析
    content_by_lua_block {
        ngx.say("<br>hello world ! welcome to openresty! i am content_by_lua_block </br>")
    }
}

5. 直接配置在nginx.conf的server下配置:include lua1.conf;

include lua1.conf;

lua1.conf脚本:

location /lua4 {
     default_type text/html; #响应内容为html解析
     content_by_lua_file conf/003.lua;
 }

003.lua脚本:

ngx.say("<br>hello world ! welcome to openresty! i am lua4</br>")

6. nginx中uri变量获取

获取单个变量:

location /lua5 {
   default_type text/html; #响应内容为html解析
   content_by_lua_block {
      ngx.say(ngx.var.arg_username) #ngx对象中->变量->参数->_下划线为语法->username为url请求参数名
   }
}

请求:http://10.4.xx.xx/lua5?username=jllasdfasdfsadf

 获取多个变量:

006.lua脚本:

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/>")   #table:lua中的表,类似于数组。如:get请求中有多个同名参数不同值的,会通过这个函数来处理
    else
        ngx.say(k," : ",v,"<br/>")
    end
end

 nginx.conf中配置:

 location /lua6 {
     default_type text/html; #响应内容为html解析
     content_by_lua_file conf/006.lua;
}

请求:http://10.4.xx.xx/lua6?username=jll&school=xxSchool&age=18&class=math&class=eglish&class=java

 7. 获取请求头的信息

007.lua脚本:

local headers = ngx.req.get_headers()
ngx.say("Host : ", headers["Host"], "<br/>")
ngx.say("user-agent : ", headers["user-agent"], "<br/>")
ngx.say("user-agent : ", headers.user_agent, "<br/>")
ngx.say("-------------------------------------------","<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

 nginx.conf脚本:

location /lua7 {
    default_type text/html; #响应内容为html解析
    content_by_lua_file conf/007.lua;
}

请求:http://10.4.xx.x/lua7

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荆茗Scaler

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值