以下配置均在/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(" 2. 调用lua脚本文件:content_by_lua_file
hello world ! welcome to openresty!")'; # content_by_lua:调用lua脚本} location /lua1 {default_type text/html; #响应内容为html解析content_by_lua_file conf/001.lua; # content_by_lua_file:调用lua脚本 } 001.lua脚本内容:
ngx.say(" 3. 调用lua脚本文件:include
hello world ! welcome to openresty!")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(" 4. 调用lua脚本:content_by_lua_block
hello world ! welcome to openresty! i am lua2")location /lua3 {default_type text/html; #响应内容为html解析content_by_lua_block {ngx.say(" 5. 直接配置在nginx.conf的server下配置:include lua1.conf;
hello world ! welcome to openresty! i am content_by_lua_block ")}}include lua1.conf; lua1.conf脚本:
location /lua4 {default_type text/html; #响应内容为html解析content_by_lua_file conf/003.lua; } 003.lua脚本:
ngx.say(" 6. nginx中uri变量获取 获取单个变量:
hello world ! welcome to openresty! i am lua4")
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) doif type(v) == "table" thenngx.say(k," : ",table.concat(v,","),"nginx.conf中配置:
")#table:lua中的表,类似于数组 。如:get请求中有多个同名参数不同值的,会通过这个函数来处理elsengx.say(k," : ",v,"
")endend
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"], "nginx.conf脚本:
")ngx.say("user-agent : ", headers["user-agent"], "
")ngx.say("user-agent : ", headers.user_agent, "
")ngx.say("-------------------------------------------","
")for k,v in pairs(headers) doif type(v) == "table" thenngx.say(k," : ",table.concat(v,","),"
")elsengx.say(k," : ",v,"
")endend
location /lua7 {default_type text/html; #响应内容为html解析content_by_lua_file conf/007.lua;} 请求:http://10.4.xx.x/lua7
【nginx-openresty-lua使用】
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
