Nginx logformat json 구조로 남기기
IT/Server
2023. 2. 3. 10:39
728x90
nginx 로그를 카프카로 전송에 용이하게 로그를 json 구조로 남길 필요가 있었다.
json 구조로 남기는 부분을 기록한다.
nginx에서 지원하는 변수 리스트
http://nginx.org/en/docs/varindex.html
nginx 로그 sample
http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
실제 작업시 nginx 설정
http {
log_format json_format escape=json '{"proxy_add_x_forwarded_for":"$proxy_add_x_forwarded_for",'
'"remote_user_ip":"$remote_user",'
'"time_local":"[$time_local]",'
'"request":"$request",'
'"status":"$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"request_time":"$request_time",'
'"upstream_response_time":"$upstream_response_time",'
'"userid":"$cookie__userId",'
'"request_body":"$request_body"}';
}
format을 지정한다.
access_log /var/log/nginx/admin-nata-access.log json_format;
실제 로그가 json구조로 남는것을 확인 할 수 있다.