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

一)header_filter_by_lua

语法:header_filter_by_lua <lua-script-str>
语境:http,server,location,location if
阶段:output-header-filter
一般用来设置cookie和headers,在该阶段不能使用如下几个API:
1、output API(ngx.say和ngx.send_headers)
2、control API(ngx.exit和ngx.exec)
3、subrequest API(ngx.location.capture和ngx.location.capture_multi)
4、cosocket API(ngx.socket.tcp和ngx.req.socket)

案例一:

location / {
    header_filter_by_lua 'ngx.header.Foo = "blah"';
    echo "Hello World!";
}

案例二:

http {  
 
    log_format  main  '$msec $status $request $request_time '  
                      '$http_referer $remote_addr [ $time_local ] '  
                      '$upstream_response_time $host $bytes_sent '  
                      '$request_length $upstream_addr';  
 
    access_log  logs/access.log main buffer=32k flush=1s;  
 
    upstream remote_world {  
        server 127.0.0.1:8080;  
    }  
 
    server {  
        listen 80;  
 
        location /exec {  
            content_by_lua '  
                local cjson = require "cjson"  
                local headers = {  
                    ["Etag"] = "662222165e216225df78fbbd47c9333",  
                    ["Last-Modified"] = "Sat, 24 July 2019 16:58:22 GMT",  
                }  
                ngx.var.my_headers = cjson.encode(headers)  
                ngx.var.my_upstream = "remote_world"  
                ngx.var.my_uri = "/world"  
                ngx.exec("/upstream")  
            ';  
        }  
 
        location /upstream {  
            internal;  
 
            set $my_headers $my_headers;  
            set $my_upstream $my_upstream;  
            set $my_uri $my_uri;  
            proxy_pass http://$my_upstream$my_uri;  
 
            header_filter_by_lua '  
                local cjson = require "cjson"  
                headers = cjson.decode(ngx.var.my_headers)  
                for k, v in pairs(headers) do  
                    ngx.header[k] = v  
                end  
            ';  
        }  
    }  
 
 
    server {  
        listen 8080;  
 
        location /world {  
            echo "hello world";  
        }  
    }  
}

二)body_filter_by_lua
语法:body_filter_by_lua <lua-script-str>
语境:http,server,location,location if
阶段:output-body-filter
输入的数据时通过ngx.arg[1](作为lua的string值),通过ngx.arg[2]这个bool类型表示响应数据流的结尾。

这个指令可以用来篡改http的响应正文的;会调用几次
在该阶段不能利用如下几个API:
1、output API(ngx.say和ngx.send_headers)
2、control API(ngx.exit和ngx.exec)
3、subrequest API(ngx.location.capture和ngx.location.capture_multi)
4、cosocket API(ngx.socket.tcp和ngx.req.socket)

输入的数据时通过ngx.arg[1],通过ngx.arg[2]这个bool范例暗示响应数据流的末了。
基于这个原因,’eof’只是nginx的链接缓冲区的last_buf(对主requests)或last_in_chain(对subrequests)的标志。
运行以下呼吁可以当即终止运行接下来的lua代码:
return ngx.ERROR
这会将响应体截断导致无效的响应。lua代码可以通过修改ngx.arg[1]的内容将数据传输到下游的
nginx output body filter阶段的其它模块中去。譬喻,将response body中的小写字母举办反转,我们可以这么写:

案例一

location /t {
    echo hello world12;
    echo hi yile;
    body_filter_by_lua '
        ngx.log(ngx.ERR,"ngx.arg[1]=",ngx.arg[1]," arg[2]=",ngx.arg[2])
        ngx.arg[1] = string.upper(ngx.arg[1])
    ';
}

尽管只有两个 echo,但是 body_filter_by_lua* 会被调用三次!

第三次调用的时候,ngx.arg[1] 为空字符串,而 ngx.arg[2] 为 true。
这是因为,Nginx 的 upstream 相关模块,以及 OpenResty 的 content_by_lua,
会单独发送一个设置了 last_buf 的空 buffer,来表示流的结束。这算是一个约定俗成的惯例,所以有必要在运行相关逻辑之前,
检查 ngx.arg[1] 是否为空。当然反过来不一定成立,ngx.arg[2] == true 并不代表 ngx.arg[1] 一定为空。

案例二

location /t {
    echo hello world;
    echo hiya globe;

body_filter_by_lua '
        ngx.log(ngx.ERR,"ngx.arg[1]=",ngx.arg[1]," arg[2]=",ngx.arg[2])
        local chunk = ngx.arg[1]
        if string.match(chunk, "hello") then
            ngx.arg[2] = true  -- new eof
            return
        end

ngx.arg[1] = nil
    ';
}

这是因为,当body filter看到了一块包括”hello“的字符块后当即将”eof“标志配置为了true,
从而导致响应被截断了但仍然是有效的回覆。

三)log_by_lua,log_by_lua_file

在log阶段指定的lua日志,并不会替换access log,而是在那之后调用。

在该阶段不能利用如下几个API:
1、output API(ngx.say和ngx.send_headers)
2、control API(ngx.exit和ngx.exec)
3、subrequest API(ngx.location.capture和ngx.location.capture_multi)
4、cosocket API(ngx.socket.tcp和ngx.req.socket)

可以利用此阶段,把日志统一收集到日志服务器中

location / {
    echo "Hello World!";
    log_by_lua_block {
        ngx.log(ngx.ERR,msg)
    }
}
elk日志收集分析

转载于:https://www.cnblogs.com/reblue520/p/11446487.html

openresty开发系列36--openresty执行流程之6日志模块处理阶段相关推荐

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

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

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

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

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

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

  4. openresty开发系列33--openresty执行流程之2重写赋值阶段

    openresty开发系列33--openresty执行流程之2重写赋值阶段 一)重写赋值阶段 1)set_by_lua 语法:set_by_lua $res <lua-script-str&g ...

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

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

  6. openresty开发系列20--lua的时间操作

    openresty开发系列20--lua的时间操作 在 Lua 中,函数 time.date 和 difftime 提供了所有的日期和时间功能. 在 OpenResty 的世界里,不推荐使用这里的标准 ...

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

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

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

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

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

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

最新文章

  1. 角谷定理python每次输出数_角谷定理C++递归问题,求问步数为什么总输出0?
  2. Nginx之进程间的通信机制-Channel
  3. 154. Find Minimum in Rotated Sorted Array II
  4. Ubuntu安装之python开发
  5. html的id不能有.吗,html – 哪些DOM元素不能接受id?
  6. 【AI视野·今日Robot 机器人论文速览 第二十期】Thu, 8 Jul 2021
  7. 协同过滤算法_基于用户的协同过滤推荐算法原理和实现
  8. LeetCode 76. 最小覆盖子串 (滑动窗口哈希表)
  9. 活学活用pic单片机c语言编程pdf,活学活用PIC单片机C语言编程
  10. 服务器tcpip修复工具,tcpip协议修复工具winsockfix
  11. 学生社团管理系统(Java+Swing+mysql)(超简陋)
  12. 修改Google桌面搜索索引位置
  13. WinRAR是什么?------压缩工具
  14. Unity3D音效问题
  15. Win10本地配置完hadoop_home任然报错Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
  16. 智能呼叫系统之客户互动中心
  17. web网页设计期末课程大作业——汉中印象旅游景点介绍网页设计与实现19页面HTML+CSS+JavaScript
  18. uniapp 封装storage缓存
  19. 【建站笔记】apache配置赛门铁克免费ssl证书搭建https
  20. 宇视NVR录像机 一直滴滴报警,如何关闭

热门文章

  1. Python学习--最完整的基础知识大全
  2. Apollo分布式配置中心在本地的安装教程
  3. python3 xpath_Python3使用Xpath解析网易云音乐歌手页面
  4. 时问轴php,php-发布到时间轴-过去的日期
  5. 率先通过信通院容器规模化测评 阿里云获最高认证级别
  6. string java getbytes_从 String.getBytes 理解 Java 编码和解码
  7. echart 时间滚动_基于 ECharts 封装甘特图并实现自动滚屏
  8. mysql中in查询效率低的替代方法_一波骚操作,我把 SQL 执行效率提高了 10,000,000 倍...
  9. pandas的自带数据集_盘点 | Python自带的那些数据集
  10. effective typescript_初学typescript(一) - 来亦何哀