Nginx access_log日志添加返回字段

主要为方便单用户请求日志回溯分析   记录用户标记。 将用户信息打印在access_log 日志里。

步骤:

1、重写nginx 的log格式

     一般是地址是  /etc/nginx/conf.d/nginx_log_format.conf 

     自定义需要记录的字段

     相关官方信息:

$upstream_http_name

keep server response header fields. For example, the “Server” response header field is available through the $upstream_http_server variable. The rules of converting header field names to variable names are the same as for the variables that start with the “$http_” prefix. Only the header fields from the response of the last server are saved.

log_format logstash '{ "http_host": "$http_host", "source_addr": "$http_x_forwarded_for", "http_code": "$status",  "bytes_sent": "$bytes_sent" , "hostname": "$hostname" , "player_id":"$upstream_http_player_id"

 这里我加的是player_id  

2、 在代码中设置 http response 的 header 值

以PHP 为例 :

header("player_id: xxxx ");

 3、最后将格式配置到对应的server  access_log 中。

server {
    listen  80;
    server_name  xxxxx;
    charset utf-8;
    root   "xxxxx";
    access_log   /data/log/nginx/alice.access.log  logstash;
    error_log    /data/log/nginx/alice.error.log;
}

 效果:

这样我们就能在access_log 日志中记录玩家信息。  将日志同步到ES 中。我们就能更好的统计用户信息。 

原文地址:https://www.cnblogs.com/glory-jzx/p/11283636.html