再说nginx记录日志、变量、post body/payload数据

网上查了查nginx记录日志资料,这里记录一下,提供大家学习和参考。

一,nginx的日志类型

1,包括

error.log: 记录nginx处理Http请求的错误的状态,以及nginx本身服务的运行错误状态,按照不同的级别进行记录。。

access_log:记录每一次Http请求的访问状态,用于分析每一次请求和交互,行为进行分析,依赖于log_format实现。

log_format:将nginx中的变量组织在一起,记录到access_log中。

2,默认配置

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

二,语法

三,nginx变量

1,http请求变量

$arg_parameter:请求参数

$http_header :请求头

$send_http_header:响应头

2,nginx内置变量

内置变量存放在  ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的。

总而言之,这些变量代表着客户端请求头的内容,例如$http_user_agent, $http_cookie, 等等。

下面是nginx支持的所有内置变量:
$arg_name:请求中的的参数名,即“?”后面的arg_name=arg_value形式的arg_name
$args:请求中的参数值
$binary_remote_addr:客户端地址的二进制形式, 固定长度为4个字节
$body_bytes_sent:传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的“%B”参数保持兼容
$bytes_sent:传输给客户端的字节数 (1.3.8, 1.2.5)
$connection:TCP连接的序列号 (1.3.8, 1.2.5)
$connection_requests:TCP连接当前的请求数量 (1.3.8, 1.2.5)
$content_length:“Content-Length” 请求头字段
$content_type:“Content-Type” 请求头字段
$cookie_name:cookie名称
$document_root:当前请求的文档根目录或别名
$document_uri:同 $uri
$host:优先级如下:HTTP请求行的主机名>”HOST”请求头字段>符合请求的服务器名
$hostname:主机名
$http_name:匹配任意请求头字段; 变量名中的后半部分“name”可以替换成任意请求头字段,如在配置文件中需要获取http请求头:“Accept-Language”,那么将“-”替换为下划线,大写字母替换为小写,形如:$http_accept_language即可。
$https:如果开启了SSL安全模式,值为“on”,否则为空字符串。
$is_args:如果请求中有参数,值为“?”,否则为空字符串。
$limit_rate:用于设置响应的速度限制,详见 limit_rate。
$msec:当前的Unix时间戳 (1.3.9, 1.2.6)
$nginx_version:nginx版本
$pid:工作进程的PID
$pipe:如果请求来自管道通信,值为“p”,否则为“.” (1.3.12, 1.2.7)
$proxy_protocol_addr:获取代理访问服务器的客户端地址,如果是直接访问,该值为空字符串。(1.5.12)
$query_string:同 $args
$realpath_root:当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径。
$remote_addr:客户端地址
$remote_port:客户端端口
$remote_user:用于HTTP基础认证服务的用户名
$request:代表客户端的请求地址
$request_body:客户端的请求主体,此变量可在location中使用,将请求主体通过proxy_pass, fastcgi_pass, uwsgi_pass, 和 scgi_pass传递给下一级的代理服务器。
$request_body_file:将客户端请求主体保存在临时文件中。文件处理结束后,此文件需删除。如果需要之一开启此功能,需要设置client_body_in_file_only。如果将次文件传递给后端的代理服务器,需要禁用request body,即设置proxy_pass_request_body off,fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off 。
$request_completion:如果请求成功,值为”OK”,如果请求未完成或者请求不是一个范围请求的最后一部分,则为空。
$request_filename:当前连接请求的文件路径,由root或alias指令与URI请求生成。
$request_length:请求的长度 (包括请求的地址, http请求头和请求主体) (1.3.12, 1.2.7)
$request_method:HTTP请求方法,通常为“GET”或“POST”
$request_time:处理客户端请求使用的时间 (1.3.9, 1.2.6); 从读取客户端的第一个字节开始计时。
$request_uri:这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI,不包含主机名,例如:”/cnphp/test.php?arg=freemouse”。
$scheme:请求使用的Web协议, “http” 或 “https”
$sent_http_name:可以设置任意http响应头字段; 变量名中的后半部分“name”可以替换成任意响应头字段,如需要设置响应头Content-length,那么将“-”替换为下划线,大写字母替换为小写,形如:$sent_http_content_length 4096即可。
$server_addr:服务器端地址,需要注意的是:为了避免访问linux系统内核,应将ip地址提前设置在配置文件中。
$server_name:服务器名,www.cnphp.info
$server_port:服务器端口
$server_protocol:服务器的HTTP版本, 通常为 “HTTP/1.0” 或 “HTTP/1.1”
$status:HTTP响应代码 (1.3.2, 1.2.2)
$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space:客户端TCP连接的具体信息
$time_iso8601:服务器时间的ISO 8610格式 (1.3.12, 1.2.7)
$time_local:服务器时间(LOG Format 格式) (1.3.12, 1.2.7)
$uri:请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改,$uri不包含主机名,如”/foo/bar.html”。

3,自定义变量

期望:在access_log打印自定义变量define_error_code

nginx配置:

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    log_format main '[$time_local] $request $status $remote_addr $define_error_code';
    server {
        listen       80;
        location / {
            set $define_error_code '';
            content_by_lua_block {
                ngx.var.define_error_code = 9527
                ngx.say("hello world")
            }
        }
        access_log logs/access.log main;
        error_log logs/error.log;
    }
}

 

四、nginx记录post body/payload数据

1. 文档

   在nginx中想利用$request_body命令获取post请求的body参数,并落日志,但是发现该变量值为空,查看官网中对$request_body的描述如下:

$request_body
    request body

    The variable’s value is made available in locations processed by the proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass directives when the request body was read to a memory buffer.

意思是只有location中用到proxy_pass,fastcgi_pass,scgi_pass命令时,该变量才有值

2.获取$request_body值

使用proxy_pass,fastcgi_pass, scgi_pass等命令

试了下用proxy_pass,的确可以。配置如下:

log_format main_post '$remote_addr\t$remote_user\t[$time_local]\t"$request"\t$status\t$bytes_sent\t'
                '"$http_referer"\t"$http_user_agent"\t"$http_x_forwarded_for"\t"$request_body"';
worker_processes  1;        #nginx worker 数量
error_log logs/error.log;   #指定错误日志文件路径
events {
    worker_connections 1024;
}
 
http {
    log_format  dm  ' "$request_body" ';
    upstream bk_servers_2 {
        server 127.0.0.1:6699;
    }
 
    server {
        listen 6699;
        location /post/ {
            proxy_pass http://bk_servers_2/api/log/letv/env;
            access_log /home/shuhao/openresty-test/logs/post.log dm;
        }
        location /api/log/letv/env {
            return 202;
        }
    }
}

使用curl命令模拟post请求

curl -i  -d "arg1=1&arg2=2" "http://127.0.0.1:6699/post/"

日志用打印出结果:

 "arg1=1&arg2=2" 

3.使用lua获取$request_body值

条件:使用openresty或者nginx编译了lua模块。

方法:

    server中使用lua_need_request_body on; 或者在location lua代码块中使用 ngx.req.read_body()

注意:

    1)lua代码块中必须有执行语句,否则lua不执行,无法获取request_body;

    2)不要使用return 200;等命令,有return命令,lua代码不执行。

worker_processes  4;        #nginx worker 数量
error_log ~/openresty-test/logs/error.log debug;   #指定错误日志文件路径
events {
    worker_connections 1024;
}
 
http {
    log_format  dm  '"$request_body"';
    lua_need_request_body on;
    server {
        listen 6699;
        location /post/ {
            content_by_lua '
                ngx.say("-------")
                ngx.req.read_body()
            ';
            access_log ~/openresty-test/logs/post.log dm;
            #return 200;
        }
    }
}

4. 自定义变量存放request body

方法:

    1)在server 块中使用set $resp_body ""; 声明变量;

    2)在location使用  ngx.var.resp_body = ngx.req.get_body_data() or "-"    为变量赋值

worker_processes  1;        #nginx worker 数量
error_log /home/shuhao/openresty-test/logs/error.log debug;   #指定错误日志文件路径
events {
    worker_connections 1024;
}
 
http {
    log_format  dm  ' "$request_body"  --  "$resp_body"';
    lua_need_request_body on;
    server {
        listen 6699;
        set $resp_body "";
        location /post/ {
            lua_need_request_body on;
            content_by_lua '
                local resp_body = ngx.req.get_body_data() or "-"                                                                                                    
                ngx.var.resp_body = resp_body
            ';
            access_log /home/shuhao/openresty-test/logs/post.log dm;
            #return 200;
        }
    }
}

5.打印nginx header和body

场景

参考https://segmentfault.com/a/1190000000606867可以获取response的报文体,由于业务测试有获取响应头Header或响应体Body的需求,这里是通过header_filter_by_lua来分配响应报文头给变量实现的。

nginx配置

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" $request_time req_body:"$request_body"' 
        ' resp_body:"$resp_body" resp_header:"$resp_header"';

    server {
        listen 8082;
        access_log logs/access.log log_req_resp;

        set $resp_header "";
        header_filter_by_lua '
            local h = ngx.resp.get_headers()
            for k, v in pairs(h) do
                ngx.var.resp_header=ngx.var.resp_header..k..": "..v
            end
        ';

        lua_need_request_body on;

        set $resp_body "";
        body_filter_by_lua '
            local resp_body = string.sub(ngx.arg[1], 1, 1000)    #窃取1000字节内容
            ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
            if ngx.arg[2] then
                ngx.var.resp_body = ngx.ctx.buffered
            end
        ';

        location / {
            echo "Hello World!";
        }
    }
}

然后access日志里就有啦~

参考

https://blog.csdn.net/echizao1839/article/details/80872378

https://www.cnblogs.com/sunsky303/p/9583550.html

https://cloud.tencent.com/developer/article/1683149

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值