关于 nginx 插件 ngx_lua ,获取响应日志的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
13246864965
V2EX    问与答

关于 nginx 插件 ngx_lua ,获取响应日志的问题

  •  
  •   13246864965 2017-12-18 11:28:31 +08:00 4351 次点击
    这是一个创建于 2861 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:使用 nginx 代理获取访问应用的请求日志与响应日志

    问题描述:目前用 nginx 代理项目,运用 ngx_lua 可以获取到请求日志, 响应日志如果是静态的网页(例<html><body>

    整个页面包括内容都可以获取

    </body></html>)可以抓取到; 如果是动态网页(前段通过 el 表达式,framework 等)就获取不到不到内容,只有静态网页;

    各位集美貌智慧于一身的 V 友们,帮忙看看解决方法或者推荐个其他方案实现,小弟高了一个星期了,在线等指导 <憋屈

    第 1 条附言    2017-12-23 09:42:01 +08:00
    问题已经解决了,在整理一下,下午有时间分享给 V 友们,有需要的 可以参考了
    第 2 条附言    2017-12-25 09:41:12 +08:00
    《 Nginx 配置环境》
    1,安装 LuaJIT
    # cd /usr/local/src
    # wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
    # tar -xzvf LuaJIT-2.0.2.tar.gz
    # cd LuaJIT-2.0.2
    # make

    出现如下内容表示编译成功
    OK Successfully built LuaJIT
    make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src'
    ==== Successfully built LuaJIT 2.0.2 ====

    # make install
    出现如下内容,表示安装成功
    ==== Successfully installed LuaJIT 2.0.2 to /usr/local ====

    2 安装 ngx_lua
    # cd /usr/local/src
    # wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.10.11.tar.gz
    # tar -xzvf v0.10.11

    3 安装 nginx
    yum install gcc-c++
    yum -y install pcre*
    yum -y install openssl*

    # cd /usr/local/src/
    # wget http://nginx.org/download/nginx-1.13.6.tar.gz
    # tar -xzvf nginx-1.13.6.tar.gz
    # cd nginx-1.13.6
    //先导入环境变量,告诉 nginx 去哪里找 luajit
    # export LUAJIT_LIB=/usr/local/lib
    # export LUAJIT_INC=/usr/local/include/luajit-2.0
    # ./configure --prefix=/usr/local/nginx-1.13.6 --add-module=../lua-nginx-module-0.8.6
    # make
    # make install

    配置文件添加
    location ~* ^/2328(/.*) {
    default_type 'text/plain';
    content_by_lua 'ngx.say("hello, ttlsa lua")';
    }
    启动测试即可,参考: http://www.ttlsa.com/nginx/nginx-modules-ngx_lua/
    第 3 条附言    2017-12-25 10:10:00 +08:00
    《 Nginx 配置 web 项目》

    log_format json_combined escape=json

    '{"serverName":"$server_name","remoteAddr":"$remote_addr","remoteUser":"$remote_user","timeLocal":"$time_l
    ocal","request":"$request","reqBody":"$request_body",'
    '"status":"$status","bodyBytesSent":"$body_bytes_sent","httpReferer":"$http_referer",'
    '"httpUserAgent":"$http_user_agent","httpXForwardedFor":"$http_x_forwarded_for",'
    "requestTime":"$request_time","upstreamResponseTime":"$upstream_respon
    se_time","respBody":"$resp_body"}';
    access_log logs/accessguo.log json_combined;

    注:escape=json 部分配置可解决中文变 16 进制问题,需要 nginx 版本 1.11.8 以上

    location1
    location /jrebot {
    rewrite ^/(.*)$ /$1 break;
    root html;
    index index.html index.htm;
    proxy_pass http://172.10.14.13:8081/jrebot;
    proxy_set_header Host $host:$server_port;
    proxy_redirect http://192.168.220.135/ http://192.168.220.135:82/jrebot/;
    proxy_set_header X-Real-IP $remote_addr:82;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Request-Url $request_uri:82;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503
    http_504 http_404;
    subs_filter 'href="/' 'href="/jrebot/';
    subs_filter 'src="/' 'src="/jrebot/';
    lua_need_request_body on;
    body_filter_by_lua '
    local resp_body = ngx.arg[1]
    ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
    if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered
    end ';
    }
    Proxy redirect 属性用来修改重定向报文的 location 字段
    Subs_filter 提供了类似的报文数据替换功能,该模块默认不会安装,需要在编译 nginx 时添加--with-http_sub_module 参数 。参考: https://www.cnblogs.com/fanzhidongyzby/p/5194895.html
    响应日志可正常
    第 4 条附言    2017-12-25 10:12:44 +08:00
    location2
    location /CxWebClient {
    rewrite ^/(.*)$ /$1 break;
    root html;
    index index.html index.htm;
    proxy_pass http://xx.xxx.xx.xx/CxWebClient;
    proxy_set_header Host $host:$server_port;#首先说明 proxy_set_header 指令在向反向代理的后端 Web 服务器发起请求时添加指定的 Header 头信息,后端 web 服务器有多个基于域名的虚拟主机时,通过头信息 Host,用于指定请求的域名,这样后端 web 才能识别反向代理请求哪个虚拟主机处理。
    proxy_redirect http://192.168.220.xx/ http://192.168.220.xx:82/;

    proxy_set_header X-Real-IP $remote_addr:82; #允许将发送到后端的服务器请求重新定义或者增加一个字段,这个可以是变量也是文本组合。
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Request-Url $request_uri:82;
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503
    http_504 http_404; #服务器头部超时相应的各种状态

    header_filter_by_lua_block {
    ngx.header.content_length = nil;
    }
    lua_need_request_body on;
    body_filter_by_lua '
    local resp_body = ngx.arg[1]
    ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
    if ngx.arg[2] then
    ngx.var.resp_body = ngx.ctx.buffered
    end
    ';
    }

    发现 get 请求响应日志正常,post 请求乱码
    在 location 配置 default_type 'application/octet-stream; charset='; 依然错误
    添加 proxy_set_header Content-Type "text/asp; charset=";get 异常,post 正常
    配置 log_escape_non_ascii off;(未验证)
    第 5 条附言    2017-12-25 10:15:04 +08:00
    最后在浏览器 get.post 的请求头发现问题:
    post 请求的 Response Header 多了 Content-Encoding:gzip;
    添加 proxy_set_header Accept-Encoding "none";
    proxy_set_header Content-Encoding "none";
    rewrite_by_lua '
    ngx.header["Accept-Encoding"]="none"
    ngx.header["Content-Encoding"]="none"
    ';
    header_filter_by_lua_block {
    ngx.header.content_length = nil;
    ngx.header["Accept-Encoding"]="none"
    ngx.header["Content-Encoding"]="none"

    }

    问题解决;
    参考 https://github.com/openresty/lua-nginx-module/blob/master/README.markdown
    该文档全英文,反复尝试了很多遍,不停地尝试终于有所收获;
    小白是做研发工作,配置也没有经验,分享出来仅供大家参考,哪里配置有误可以指出,一起分享,一起进步。。。( PS:今天刚好 12,25,祝大家圣诞节快乐,工作顺顺利利!!!)
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1287 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 17:20 PVG 01:20 LAX 10:20 JFK 13:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86