오늘의 인기 글
최근 글
최근 댓글
Today
Total
05-04 00:00
관리 메뉴

우노

[Nginx] Json 로그 포맷 설정 본문

Web/Nginx

[Nginx] Json 로그 포맷 설정

운호(Noah) 2023. 3. 11. 15:08

들어가기 앞서,

  • 해당 포스트에선, Nginx 액세스 로그를 Json 형식으로 남기는 방법에 대해서 다뤄보겠습니다.

Nginx 기본 로그 포맷

log_format  main  
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

Nginx Json 로그 포맷 설정

  • 아래 코드를 참고해, nginx.conf 파일 내부 http 블락에 원하는 variables로 로그 포맷을 설정하면 됩니다.

      # Json 로그 포맷 설정
      log_format nginxlog_json escape=json
      '{'
          '"http_user_agent":"$http_user_agent",'
          '"http_cookie":"$http_cookie",'
          '"arg_name":"$arg_name",'
          '"args":"$args",'
          '"body_bytes_sent":"$body_bytes_sent",'
          '"bytes_sent":"$bytes_sent",'
          '"connection":"$connection",'
          '"connection_requests":"$connection_requests",'
          '"content_length":"$content_length",'
          '"content_type":"$content_type",'
          '"cookie_name":"$cookie_name",'
          '"document_root":"$document_root",'
          '"document_uri":"$document_uri",'
          '"host":"$host",'
          '"hostname":"$hostname",'
          '"http_name":"$http_name",'
          '"https":"$https",'
          '"is_args":"$is_args",'
          '"limit_rate":"$limit_rate",'
          '"msec":"$msec",'
          '"nginx_version":"$nginx_version",'
          '"pid":"$pid",'
          '"pipe":"$pipe",'
          '"proxy_protocol_addr":"$proxy_protocol_addr",'
          '"proxy_protocol_port":"$proxy_protocol_port",'
          '"query_string":"$query_string",'
          '"realpath_root":"$realpath_root",'
          '"remote_addr":"$remote_addr",'
          '"remote_port":"$remote_port",'
          '"remote_user":"$remote_user",'
          '"request":"$request",'
          '"request_body":"$request_body",'
          '"request_body_file":"$request_body_file",'
          '"request_completion":"$request_completion",'
          '"request_filename":"$request_filename",'
          '"request_id":"$request_id",'
          '"request_length":"$request_length",'
          '"request_method":"$request_method",'
          '"request_time":"$request_time",'
          '"request_uri":"$request_uri",'
          '"scheme":"$scheme",'
          '"sent_http_name":"$sent_http_name",'
          '"server_addr":"$server_addr",'
          '"server_name":"$server_name",'
          '"server_port":"$server_port",'
          '"server_protocol":"$server_protocol",'
          '"status":"$status",'
          '"tcpinfo_rtt":"$tcpinfo_rtt",'
          '"tcpinfo_rttvar":"$tcpinfo_rttvar",'
          '"tcpinfo_snd_cwnd":"$tcpinfo_snd_cwnd",'
          '"tcpinfo_rcv_space":"$tcpinfo_rcv_space",'
          '"time_iso8601":"$time_iso8601",'
          '"time_local":"$time_local",'
          '"uri":"$uri"'
      '}';
    
      # 액세스 로그를 파일로 저장하고 싶다면
      access_log /var/log/nginx/access.log nginxlog_json;
    
      # 액세스 로그를 stdout으로 출력하고 싶다면
      # access_log /dev/stdout nginxlog_json;
  • 다양한 variables는 아래 링크를 참고하시면 됩니다.

참고

'Web > Nginx' 카테고리의 다른 글

[Nginx] proxy_pass 시 cookie 전달  (0) 2023.03.30
[Nginx] proxy_pass 시 custom header 추가  (0) 2023.03.28
[Nginx] 변수 사용 방법  (0) 2023.03.27
[Nginx] resolver란?  (0) 2023.02.23
[Nginx] 정규표현식  (1) 2023.02.17
Comments