对nginx反向代理日志json格式化

小木兮子 2020-06-12

反向代理日志json

步骤

  1. 使用ansible用户登录反向代理服务器

cd /etc/nginx/

  1. 修改配置文件nginx.conf

sudo vi nginx.conf

log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                    ‘$status $body_bytes_sent "$http_referer" ‘
                    ‘"$http_user_agent" "$http_x_forwarded_for"‘;
?
access_log /var/log/nginx/access.log main;

改为

log_format json ‘{"request_id": "$request_id",‘
                    ‘"remote_addr": "$remote_addr",‘
                    ‘"time_iso8601": "$time_iso8601",‘
                    ‘"request": "$request",‘
                    ‘"status": "$status",‘
                    ‘"body_bytes_sent": "$body_bytes_sent",‘
                    ‘"http_referer": "$http_referer",‘
                    ‘"http_user_agent": "$http_user_agent",‘
                    ‘"http_x_forwarded_for": "$http_x_forwarded_for",‘
                    ‘"request_time": "$request_time",‘
                    ‘"upstream_response_time": "$upstream_response_time",‘
                    ‘"upstream_addr": "$upstream_addr",‘
                    ‘"upstream_connect_time": "$upstream_connect_time",‘
                    ‘"upstream_status": "$upstream_status" }‘;
?
access_log /var/log/nginx/access.log json;

  1. 重新加载nginx

sudo nginx -t
?
sudo nginx -s reload

相关推荐