主要為方便單用戶請求日志回溯分析 記錄用戶標記。 將用戶信息打印在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 中。我們就能更好的統計用戶信息。