nginx 日志log_format格式
官方文档:
http://nginx.org/en/docs/http/ngx_http_log_module.html
The ngx_http_log_module module writes request logs in the specified format.
Requests are logged in the context of a location where processing ends. It may be different from the original location, if an internal redirect happens during request processing.
Example Configuration
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"'; access_log /spool/logs/nginx-access.log compression buffer=32k;
Nginx大致有三类变量能够记录在log_format 中
- HTTP请求变量- arg_PARAMETER http_HEADER send_http_HEADER(服务端返回)
- 内置变量- Nginx内置的
- 自定义变量- 自己定义的
我们看一下默认的log_format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
我们看到默认的格式,基本都是单引号 '' 包裹着一些变量,还包括 中划线 - 方括号 [] 作为分隔符一起打印。
每个变量都有这含义:
remote_addr:对应客户端的地址
remote_user:是请求客户端请求认证的用户名,如果没有开启认证模块的话是值为空。
time_local:表示nginx服务器时间
request:表示request请求头的行
status:表示response的返回状态
body_bytes_sent:表示从服务端返回给客户端的body数据大小
http_referer:表示请求的上一级页面
http_user_agent:表示agent信息
http_x_forwarded_for:会记录每一级请求中信息
1、log_format 普通格式
|
1
2
3
|
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" "$upstream_response_time"'; |
2、log_format JSON 格式
为了便于利用 Elastic Stack 日志平台收集展示 Nginx 的日志,可以将 Nginx 的日志改成 json 的格式。修改后的 json 日志格式如下所示:
在 Nginx 的配置文件nginx.conf中,我们定义了两种的日志格式:main和log_json,其中,main为普通的文本格式,log_json为 json 格式。log_json其实就是手工构造一个 json 字符串。定义了 json 的日志格式后,便可以指定 access log 为 json 格式:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
log_format logJson '{ "@timestamp": "$time_local", ' '"@fields": { ' '"remote_addr": "$remote_addr", ' '"remote_user": "$remote_user", ' '"body_bytes_sent": "$body_bytes_sent", ' '"request_time": "$request_time", ' '"status": "$status", ' '"request": "$request", ' '"request_method": "$request_method", ' '"http_referrer": "$http_referer", ' '"body_bytes_sent":"$body_bytes_sent", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"http_user_agent": "$http_user_agent" } }'; |
nginx的log是没有自动分割功能的。需要自己写shell脚本分割。
nginx 日志log_format格式的更多相关文章
- elk 入门 - 分析nginx日志 + json格式 + 有调试的意识 + elk7.2.0
1.本次采用的一台主机,将所有的软件安装一台上进行测试工作. 2.安装部署:https://blog.51cto.com/hwg1227/2299995 3.简单调试 输出rubydebug inpu ...
- ElastAlert监控日志告警Web攻击行为---tomcat和nginx日志json格式
一.ELK安装 1.2 elk配置 logstash自定义配置文件,取名为filebeat_log.conf : input { beats { port => 5044 client_inac ...
- nginx 日志 log_format 及字段说明
1.log_format 普通格式 log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$stat ...
- ELK学习实验014:Nginx日志JSON格式收集
1 Kibana的显示配置 https://demo.elastic.co/app/kibana#/dashboard/welcome_dashboard 环境先处理干净 安装nginx和httpd- ...
- Nginx日志统一格式
统一格式如下:nginx.conf 纯文本: log_format main '$remote_addr - $remote_user [$time_local] "$request&quo ...
- 烂泥:利用awstats分析nginx日志
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 昨天把nginx的日志进行了切割,关于如何切割nginx日志,可以查看<烂泥:切割 ...
- Nginx日志分析及脚本编写
在我们日常的运维中,当Nginx服务器正常运行后,我们会经常密切关注Nginx访问日志的相关情况,发现有异常的日志信息需要进行及时处理. 那今天我将跟大家一起来研究和分析Nginx日志,nginx默认 ...
- 通过python统计nginx日志定位php网站响应慢的问题
# 公司网站反映很慢,可能是一些页面的访问方法或者页面引起,通过程序统计nginx访问日志的页面和具体的action方法访问次数以及平均响应时间可以为程序开发的同事提供参考定位具体的代码 # 默认的n ...
- ELK filter过滤器来收集Nginx日志
前面已经有ELK-Redis的安装,此处只讲在不改变日志格式的情况下收集Nginx日志. 1.Nginx端的日志格式设置如下: log_format access '$remote_addr - $r ...
随机推荐
- Ubuntu设置DNS永久生效
环境查看 设置临时生效,修改配置文件/etc/resolv.conf nameserver 202.96.134.133 重启失效 设置永久生效,修改网卡配置文件/etc/network/interf ...
- IIS Asp.Net 访问 Com组件 报拒绝访问
IIS Asp.Net 访问 Com组件 报拒绝访问 解决方法: IIS 程序池->高级设置->进程模式->标识->内置帐户=LocalSystem
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- C# 调用存储过程 Sql Server存储过程 存储过程报错,程序中的try
C#程序调用Sql Server存储过程,存储过程中报错情况,返回值... 0.SQL存储过程 USE [Opos] GO /****** Object: StoredProcedure [dbo]. ...
- [No0000173]97 条 Linux 常用命令总结
1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件-A 通-a,但不列出"."和".."-l 列 ...
- confd
1.下载安装confd ]# wget https://github.com/kelseyhightower/confd/releases/download/v0.15.0/confd-0.15.0- ...
- 2014年蓝桥杯省赛A组c++第3题(数组构造+暴力求解)
/* 标题:神奇算式 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要 ...
- 转:EL表达式的11个内置对象
原文地址:https://blog.csdn.net/qq_17045385/article/details/54799998 EL是JSP内置的表达式语言 JSP2.0开始,不让再使用Java脚本, ...
- C++生成静态库
//StaticMath.h #include <iostream> class StaticMath { public: //StaticMath(void); //~StaticMat ...
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...