从零编写日志分析系统之nginx日志配置

来源:互联网 发布:c语言单链表的创建 编辑:程序博客网 时间:2024/06/05 13:53

配置nginx日志文件

nginx原有的配置文件包含的信息比较少,可以修改nginx.conf得到更多的数据。

nginx日志文件输出形式有三种形式,最常见的就是通过空格区分字段输出。还有通过自定义json形式输出,通过syslog直接输出到远端服务。

  • 通过空格区分输出
log_format  main  '$remote_addr $remote_user [$time_local] "$request" $http_host '        '$status $upstream_status $body_bytes_sent "$http_referer"'        '"$http_user_agent$ssl_protocol $ssl_cipher $upstream_addr '        '$request_time $upstream_response_time';
  • 通过自定义json输出
 - log_format json '{"@timestamp":"$time_iso8601",''"host":"$server_addr",''"clientip":"$remote_addr",''"size":$body_bytes_sent,''"responsetime":$request_time,''"upstreamtime":"$upstream_response_time",''"upstreamhost":"$upstream_addr",''"http_host":"$host",''"url":"$uri",''"xff":"$http_x_forwarded_for",''"referer":"$http_referer",''"agent":"$http_user_agent",''"status":"$status"}';
  • 发送到远端服务
 - access_log syslog:server=unix:/data0/rsyslog/nginx.sock locallog;
  • 发送到logstarsh
access_log syslog:server=192.168.0.2:5140,facility=local6,tag=nginx_access,severity=info logstashlog;

附录:nginx日志参数说明
参考:ELK stack中文指南

原创粉丝点击