openresty运维——日志记录请求体和响应体内容

研究成果:

I、消息体解码




II、让长消息体显示完整

一、安装openresty

1、下载链接:https://openresty.org/download/openresty-1.19.9.2.tar.gz

2、破解 日志消息硬编码长度限制

sed -i '/^#define NGX_MAX_ERROR_STR/s/4096/1024000/g' ./bundle/nginx-1.19.9/src/core/ngx_log.h ./build/nginx-1.19.9/src/core/ngx_log.h

3、安装

yum install pcre-devel openssl-devel gcc curl zlib-devel
./configure --prefix=/usr/local/openresty -j2
gmake -j2
gmake install

二、配置openresty

1、nginx.conf:

worker_processes  1;
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format main escape=json '{"realip":"$remote_addr","timestamp":"$time_iso8601","request":"$request","content_type":"$sent_http_content_type","content_disposition":"$sent_http_content_disposition","referer":"$http_referer","user_agent":"$http_user_agent","req_body_size":"$content_length","req_body":"$request_body","status":"$status","resp_body_size":"$body_bytes_sent","resp_body":"$resp_body"}';
                        
    lua_package_path "/usr/local/openresty/lualib/?.lua;;";
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include vhosts/*.conf;
}

2、clcs-front.conf:

server {
    listen 9993;
    root /data/qianduan/CLCS-front/dist;
    charset utf-8;
    include /etc/nginx/default.d/*.conf;
    error_page 404 /404.html;
    location = /404.html {
    }
    error_page 500 502 503 504 /50x.html;
    set $resp_body "";
    lua_need_request_body on;
    body_filter_by_lua '
    local resp_body = string.sub(ngx.arg[1], 1, 1024000)
    ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
    if ngx.arg[2] then
    ngx.var.resp_body = ngx.ctx.buffered
    end
    ';
    location = /50x.html {
    }
    location / {
        root /data/qianduan/CLCS-front/dist;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
        error_page 405 =200 $request_uri;
    }
    location /api/ {
        proxy_http_version 1.1;
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass http://192.168.1.51:18085;
        proxy_read_timeout 1800s;
        client_max_body_size 10m;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /fileApi/ {
        proxy_http_version 1.1;
        rewrite ^/fileApi/(.*)$ /$1 break;
        proxy_pass http://118.31.4.241:11509;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /ws {
        proxy_pass http://118.31.4.241:11503; # WebSocket 后端地址
        proxy_http_version 1.1; # 使用 HTTP/1.1 以支持 WebSocket
        proxy_set_header Upgrade $http_upgrade; # 必须设置 Upgrade 头
        proxy_set_header Connection "upgrade"; # 必须设置 Connection 头
        proxy_set_header Host $host; # 传递 Host 信息
        proxy_set_header X-Real-IP $remote_addr; # 传递客户端 IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 传递客户端链路
        proxy_set_header X-Forwarded-Proto $scheme; # 传递协议 (http/https)
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
    location ~ /\.ht {
        deny all;
    }
    access_log /var/log/CLCS-front-1.log main;
    error_log /var/log/CLCS-front-1.err.log;
}

三、安装gotty:

1、下载:

https://github.com/yudai/gotty/releases/download/v1.0.1/gotty_linux_amd64.tar.gz

2、安装jq:

yum -y install jq

3、启动服务:

nohup /usr/local/openresty/bin/gotty -p 8888 -c root:szhl@2019 sh -c 'tail -n 3 -f /var/log/CLCS-front-1.log  |jq' &>/dev/null &

效果展示:

勾选单词查询100条/页,日志中的消息体显示完整,不截断:

请求体:

响应体:

Categories: 系统运维