官方文档:

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中,我们定义了两种的日志格式:mainlog_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脚本分割

最新文章

  1. HTML 基础知识
  2. Linux 多线程可重入函数
  3. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
  4. 使用wp_editor函数实现可视化编辑器
  5. Android详细的对话框AlertDialog.Builder使用方法
  6. mysql优化 mysql explain
  7. [SQL Server]一次执行资料夹内的.sql 指令码
  8. linux 命令c语言代码实现
  9. Codeforces Round #316 (Div. 2) D、E
  10. C结构体中位域
  11. 常用字符串string
  12. 如何访问 Service?- 每天5分钟玩转 Docker 容器技术(99)
  13. 4. Go常量
  14. Vue Router 路由实现原理
  15. java servlet练习测试
  16. leetcode1025
  17. 世纪互联提供的关于Powershell中将虚拟机加入备份保管库的方法
  18. SQL Server 无法连接到本地服务器
  19. Unity3D项目之 Survival Shooter 记录
  20. L130

热门文章

  1. 杭电ACM 1297 Children’s Queue
  2. Eonasdan bootstrap datetimepicker 使用记录
  3. [No0000180]改善C#程序的建议8:避免锁定不恰当的同步对象
  4. [No0000168]Excle只允许用户输入纯文本,禁止用户修改单元格样式、格式等
  5. TensorRT 进行推理
  6. 1.7Oob 方法体中的循环也能也能返回值给方法
  7. vue 错误分析
  8. 篮球游戏AI预研
  9. 【Linux】Linux 常用命令汇总
  10. 【Java】一台服务器配置多个Tomcat