Nginx access_log日志添加返回字段

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

步骤:nginx

一、重写nginx 的log格式spa

     通常是地址是  /etc/nginx/conf.d/nginx_log_format.conf 日志

     自定义须要记录的字段code

     相关官方信息:orm

$upstream_http_nameserver

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.htm

 

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  blog

 

二、 在代码中设置 http response 的 header 值utf-8

以PHP 为例 :

header("player_id: xxxx ");

 

 三、最后将格式配置到对应的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 中。咱们就能更好的统计用户信息。 

相关文章
相关标签/搜索