您的位置:首页 > 运维架构 > Nginx

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

2017-09-05 10:28 447 查看

配置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=ng
inx_access,severity=info logstashlog;


附录:nginx日志参数说明

参考:ELK stack中文指南
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: