Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change Nginx logs to JSON format

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to change the Nginx log to JSON format. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The Nginx log defaults to plain text, for example, the following is a line of access log for Nginx:

10.88.122.105-[02/Dec/2017:09:15:04 + 0800] "GET / js/pagination.js HTTP/1.1" 304 0 "http://10.88.105.20:8063/stockrecommand.html"" Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) .net 4.0C; .net 4.0E) ""-"0.000

In order to make it easier to use the Elastic Stack log platform to collect and display Nginx logs, you can change the Nginx logs to json format. The modified json log format is as follows:

{"@ timestamp": "12/Dec/2017:14:30:40 + 0800", "remote_addr": "10.88.122.108", "referer": "-", "request": "GET / HTTP/1.1", "status": 304,0, "agent": "Mozilla/5.0 (Windows NT 10.0) WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36 "," x_forwarded ":"-"," up_addr ":"-"," up_host ":"-"," up_resp_time ":"-"," request_time ":" 0.000 "}

In order to change the log format of Nginx to json, we need to modify the configuration file of Nginx. The configuration file of author Nginx is / usr/local/nginx/conf/nginx.conf.

Http {include mime.types; default_type application/octet-stream; charset utf-8; log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for" $request_time' Log_format log_json'{"@ timestamp": "$time_local",''"remote_addr": "$remote_addr",''"referer": "$http_referer",'"request": "$request",''"status": $status,''"bytes": $body_bytes_sent,''"agent": "$http_user_agent",''"x_forwarded": "$http_x_forwarded_for" "up_addr": "$upstream_addr", "up_host": "$upstream_http_host", "up_resp_time": "$upstream_response_time", "request_time": "$request_time"'}' Access_log logs/access.log log_json; (omit content)}

In Nginx's configuration file nginx.conf, we define two log formats: main and log_json, where main is a normal text format and log_json is a json format. Log_json is actually about manually constructing a json string. Once you have defined the log format for json, you can specify access log as json format:

Access_log logs/access.log log_json

Modify the configuration of Nginx, restart Nginx, you can see the log in json format, and restart Nginx:

Nginx-s reload on "how to change the Nginx log to JSON format" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report