小言_互联网的博客

nginx 配合 lua 打印响应报文和请求报文日志

911人阅读  评论(0)

下载安装LuaJIT - LuaJIT-2.0.5

# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
# tar -zxvf LuaJIT-2.0.5.tar.gz
# cd LuaJIT-2.0.5
# make && make install

下载nginx lua模块 - lua-nginx-module-0.10.10

# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.10.tar.gz
# tar -zxvf v0.10.10.tar.gz 

# export LUAJIT_LIB=/usr/local/lib 
# export LUAJIT_INC=/usr/local/include/luajit-2.0

安装 nginx 依赖

# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

下载 nginx

到这个地址可以查看所有nginx版本,选择自己需要到那一版下载
https://nginx.org/download/

这里我下载的 1.16.1 版本

安装 nginx

./configure  --prefix=/home/yunpay/nginx --with-stream --with-http_ssl_module --add-module=../lua-nginx-module-0.10.10/

make && make install

出现

./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决方法:

# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

浏览器验证

打不开网页

解决方法,关闭Linux防火墙

centOS6及以前版本使用命令: 
systemctl stop iptables.service

centOS7关闭防火墙命令: 
systemctl stop firewalld.service

报错 403 Forbidden

nginx 错误日志如下

这个问题解决方法很多,这里选择其中一种

编辑nginx.conf文件
首行 user 的 nobody 改为 root,或者其他有权限的用户名
重启nginx

成功界面

配置nginx打印响应和请求报文日志

nginx.conf 文件配置如下

user  yunpay;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #log_format main '$request_body'
    log_format main escape=json '请求报文 :$request_body | 响应报文:"$resp_body"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset koi8-r;
	    set $resp_body "";
        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /sendAndForward {
            proxy_pass  https://........:8082/sendAndForward;
            
	        lua_need_request_body on;
            body_filter_by_lua '
                local resp_body = string.sub(ngx.arg[1], 1, 1000)
                ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
                if ngx.arg[2] then
                    ngx.var.resp_body = ngx.ctx.buffered
                end
            ';
	}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

日志打印结果


转载:https://blog.csdn.net/qq_27127385/article/details/106078084
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场