论坛首页 编程语言技术论坛

Nginx 输出Json格式日志

浏览 4583 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2015-05-19  

在项目中,为了提高数据处理速度,需要Nginx输入Json格式的日志,处理方式如下:

 

修改NGINX_HOME/conf/nginx.conf文件,开启log_format格式,并配置格式定义如下:

 

log_format logstash_json '{ "@timestamp": "$time_iso8601", '
'"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", '
'"upstr_addr": "$upstream_addr",'
'"upstr_host": "$upstream_http_host",'
'"ups_resp_time": "$upstream_response_time" }';

 

 开启Access日志

 access_log  logs/access.log  logstash_json;

 

配置完成后,reload Nginx服务

sbin/nginx -s reload

 

查看输出的日志格式:

{ "@timestamp": "2015-05-19T17:20:16+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/css/bootstrap-responsive.min.css HTTP/1.1", "status": 200, "bytes":16451, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.002" }
{ "@timestamp": "2015-05-19T17:20:16+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/css/font-awesome.min.css HTTP/1.1", "status": 200, "bytes":21141, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.001" }
{ "@timestamp": "2015-05-19T17:20:16+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/css/bootstrap.light.min.css HTTP/1.1", "status": 200, "bytes":120584, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.003" }
{ "@timestamp": "2015-05-19T17:20:16+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/img/small.png HTTP/1.1", "status": 200, "bytes":480, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.002" }
{ "@timestamp": "2015-05-19T17:20:17+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/app/app.js HTTP/1.1", "status": 200, "bytes":658010, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.008" }
{ "@timestamp": "2015-05-19T17:20:17+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/", "request": "GET /qmonitor/config.js HTTP/1.1", "status": 200, "bytes":2298, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.002" }
{ "@timestamp": "2015-05-19T17:20:18+08:00", "remote_addr": "192.168.99.34", "referer": "http://10.10.20.212/qmonitor/css/font-awesome.min.css", "request": "GET /qmonitor/font/fontawesome-webfont.woff?v=3.2.1 HTTP/1.1", "status": 304, "bytes":0, "agent": "Mozilla/5.0 (Windows NT 6.1; rv:35.0) Gecko/20100101 Firefox/35.0", "x_forwarded": "-", "upstr_addr": "10.10.20.164:9480","upstr_host": "-","ups_resp_time": "0.002" }

 

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics