openresty开发系列33--openresty执行流程之2重写赋值阶段
一)重写赋值阶段
1)set_by_lua
语法:set_by_lua $res <lua-script-str> [$arg1 $arg2 …]
语境:server、server if、location、location if
阶段:rewrite
设置nginx变量,咱们用的set指令即便配合if指令也很难实现负责的赋值逻辑;
传入参数到指定的lua脚本代码中执行,并获得返回值到res中。
<lua-script-str>中的代码可使从ngx.arg表中取得输入参数(顺序索引从1开始)。
这个指令是为了执行短时间、快速运行的代码由于运行过程当中nginx的事件处理循环是处于阻塞状态的。
耗费时间的代码应该被避免。
禁止在这个阶段使用下面的API:
一、output api(ngx.say和ngx.send_headers);
二、control api(ngx.exit);
三、subrequest api(ngx.location.capture和ngx.location.capture_multi);
四、cosocket api(ngx.socket.tcp和ngx.req.socket);
五、sleep api(ngx.sleep)
a、nginx.conf配置文件
location /lua {
set $jump "1";
echo $jump;
}
set 命令对变量进行赋值,但有些场景的赋值业务比较复杂,须要用到lua脚本
因此用到set_by_lua
b、补充知识点:
ngx.var.arg与ngx.req.get_uri_args的区别,都是可以获取请求参数
ngx.var.arg_xx与ngx.req.get_uri_args["xx"]二者都是为了获取请求uri中的参数
例如 http://pureage.info?strider=1
为了获取输入参数strider,如下两种方法均可以:
local strider = ngx.var.arg_strider
local strider = ngx.req.get_uri_args["strider"]
差异在于,当请求uri中有多个同名参数时,ngx.var.arg_xx的作法是取第一个出现的值
ngx.req_get_uri_args["xx"]的作法是返回一个table,该table里存放了该参数的全部值
例如,当请求uri为:http://pureage.info?strider=1&strider=2&strider=3&strider=4时
ngx.var.arg_strider的值为"1",而ngx.req.get_uri_args["strider"]的值为table ["1", "2", "3", "4"]。
所以,ngx.req.get_uri_args属于ngx.var.arg_的加强。
-------------------------------
案例需求:书店网站改造把以前的skuid为8位商品,请求到之前的页面,为9位的请求到新的页面
书的商品详情页进行了改造,美化了一下;上线了时候,不要一会儿切换美化页面;;作AB概念
把新录入的书的商品 采用 新的商品详情页,以前维护的书的商品详情页 用老的页面
之前书的id 为8位,新的书id为9位
root /data/www/html;
location /book {
set_by_lua $to_type '
local skuid = ngx.var.arg_skuid
ngx.log(ngx.ERR,"skuid=",skuid)
local r = ngx.re.match(skuid, "^[0-9]{8}$")
local k = ngx.re.match(skuid, "^[0-9]{9}$")
if r then
return "1"
end;
if k then
return "2"
end;
';
if ($to_type = "1") {
echo "skuid为8位";
proxy_pass http://127.0.0.1/old_book/$arg_skuid.html;
}
if ($to_type = "2") {
echo "skuid为9位";
proxy_pass http://127.0.0.1/new_book/$arg_skuid.html;
}
}
# 具体的网页内容
[root@node5 data]# tree /data/www/html/
/data/www/html/
├── new_book
│ └── 123456789.html
└── old_book
└── 12345678.html
2 directories, 2 files
[root@node5 data]# cat /data/www/html/old_book/12345678.html
<h1>old book</h1>
[root@node5 data]# cat /data/www/html/new_book/123456789.html
<h1>new book</h1>
# 测试访问:http://10.11.0.215/book?skuid=123456789
会跳转到new_book/123456789.html
访问:http://10.11.0.215/book?skuid=12345678
会跳转到new_book/12345678.html
=======================================
2)set_by_lua_file
语法set_by_lua_file $var lua_file arg1 arg2...;
在lua代码中能够实现全部复杂的逻辑,可是要执行速度很快,不要阻塞;
location /lua_set_1 {
default_type "text/html";
set_by_lua_file $num /usr/local/luajit/test_set_1.lua;
echo $num;
}
2.一、test_set_1.lua
local uri_args = ngx.req.get_uri_args()
local i = uri_args["i"] or 0
local j = uri_args["j"] or 0
return i + j
获得请求参数进行相加而后返回。
访问如http://192.168.31.138/lua_set_1?i=1&j=10进行测试。 若是咱们用纯set指令是没法实现的。
3)注意,这个指令只能一次写出一个nginx变量,可是使用ngx.var接口能够解决这个问题:
location /foo {
set $diff '';
set_by_lua $sum '
local a = 32
local b = 56
ngx.var.diff = a - b; --写入$diff中
return a + b; --返回到$sum中
';
echo "sum = $sum, diff = $diff";
}html