🫠
Nginxでログをjson形式にしたらcombinedも出力されてしまった
修正前
etc/nginx/conf.d/default.confのトップレベルにlog_formatとaccess_logを追記した
log_format json escape=json '{'
'"time":"$time_iso8601",'
'"host":"$remote_addr",'
'"port":"$remote_port",'
'"method":"$request_method",'
'"uri":"$request_uri",'
'"status":"$status",'
'"body_bytes":"$body_bytes_sent",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent",'
'"request_time":"$request_time",'
'"respons_time":"$upstream_response_time"'
'}';
access_log /var/log/nginx/access.log json;
server {
listen 80;
client_max_body_size 10m;
root /public/;
location / {
proxy_set_header Host $host;
proxy_pass http://app:8080;
}
}
出力されたログはcombined(デフォルトの設定)とjsonが2行
172.28.0.1 - - [15/Sep/2023:20:38:37 +0000] "GET /image/10001.png HTTP/1.1" 200 225627 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
{"time":"2023-09-15T20:38:37+00:00","host":"xxx.xxx.xxx.xxx","port":"xxxxx","method":"GET","uri":"/image/10001.png","status":"200","body_bytes":"225627","referer":"http://localhost/","ua":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36","request_time":"0.007","respons_time":"0.007"}
修正後
access_logディレクティブはserver配下に記述したらjsonのみ出力されるようになった
log_format json escape=json '{'
'"time":"$time_iso8601",'
'"host":"$remote_addr",'
'"port":"$remote_port",'
'"method":"$request_method",'
'"uri":"$request_uri",'
'"status":"$status",'
'"body_bytes":"$body_bytes_sent",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent",'
'"request_time":"$request_time",'
'"respons_time":"$upstream_response_time"'
'}';
server {
listen 80;
client_max_body_size 10m;
root /public/;
location / {
proxy_set_header Host $host;
proxy_pass http://app:8080;
}
access_log /var/log/nginx/access.log json;
}
Discussion