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:
1、output api(ngx.say和ngx.send_headers);
2、control api(ngx.exit);
3、subrequest api(ngx.location.capture和ngx.location.capture_multi);
4、cosocket api(ngx.socket.tcp和ngx.req.socket);
5、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.1、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";
}

openresty开发系列33--openresty执行流程之2重写赋值阶段的更多相关文章

  1. openresty开发系列36--openresty执行流程之6日志模块处理阶段

    openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...

  2. openresty开发系列35--openresty执行流程之5内容content阶段

    openresty开发系列35--openresty执行流程之5内容content阶段 content 阶段 ---init阶段---重写赋值---重写rewrite---access content ...

  3. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  4. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...

  5. openresty开发系列32--openresty执行流程之1初始化阶段

    openresty开发系列32--openresty执行流程之初始化阶段 一)初始化阶段 1)init_by_lua   init_by_lua_block     init_by_lua_file语 ...

  6. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  7. openresty开发系列38--通过Lua+Redis 实现动态封禁IP

    openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...

  8. openresty开发系列37--nginx-lua-redis实现访问频率控制

    openresty开发系列37--nginx-lua-redis实现访问频率控制 一)需求背景 在高并发场景下为了防止某个访问ip访问的频率过高,有时候会需要控制用户的访问频次在openresty中, ...

  9. openresty开发系列31--openresty执行流程

    openresty开发系列31--openresty执行流程 我们先看个例子 location /test {    set $a 32;    echo $a;    set $a 56;    e ...

随机推荐

  1. RocketMQ-Console安装

    1.获取源码 git clone -b release-rocketmq-console- https://github.com/apache/rocketmq-externals.git 2.进入工 ...

  2. pgrep,pkill

    pgrep, pkill - look up or signal processes based on name and other attributes 根据名称和其它属性来查找进程 pgrep: ...

  3. Python,for循环小例子--99乘法表

    一.99乘法表 for i in range(1, 10): for j in range(1, i + 1): print('%sx%s=%s ' % (j, i, j * i), end='') ...

  4. 今天看了《SOFT SKILLS The Software Developer's Life Manual》有感

    从第四篇生产力开始看的,书中提到了专注,待续

  5. Windows下安装Scipy和Numpy失败的解决方案

    使用 pip 安装 Scipy 库时,经常会遇到安装失败的问题 pip install numpy pip install scipy 后来网上搜寻了一番才得以解决.scipy 库需要依赖 numpy ...

  6. git remote 命令的用法

    查看关联的远程仓库信息 # 查看关联的远程仓库的名称    git remote    # 查看关联的远程仓库的详细信息    git remote -v 添加远程仓库的关联 远程仓库的名称一般默认为 ...

  7. X.509

    参考:https://baike.baidu.com/item/X.509/2817050?fr=aladdin 标题:X.509  百度百科 在密码术中,X.509是定义公用密钥证书格式的标准. X ...

  8. Django项目中使用plupload插件实现上传图片功能

    首先下载plupload插件放在static静态文件下面,官方地址:https://www.plupload.com/ 项目根目录下创建media文件夹用来存放上传的图片,配置settings文件,添 ...

  9. UVA1537 Picnic Planning(思维+最小生成树)

    将1号点从图中去掉过后,图会形成几个连通块,那么我们首先可以在这些连通块内部求最小生成树. 假设有\(tot\)个连通块,那么我们会从1号点至少选\(tot\)个出边,使得图连通.这时我们贪心地选择最 ...

  10. netstat -an unix socket 会阻塞吗

    [lyd@localhost ~]$ netstat -an | grep "SOFO"unix 2 [ ACC ] SEQPACKET LISTENING 86308 @*MY- ...