Nginx的日志log_format - Go语言中文社区

Nginx的日志log_format


1.HTTP请求

大家都知道nginx作为webserver和http的代理,处理的就是http请求。
http请求是是建立在tcp基础上的。
一个完整的http请求包括request和response两部分。
request组成:请求行,请求头,请求数据
response组成:状态行,消息报头,响应正文
我们可以在命令行查看一个完整的http请求:

 curl -v https://coding.imooc.com > /dev/null

> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: coding.imooc.com
> Accept: */*
> 
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 22 Jan 2018 13:38:31 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Vary: Accept-Encoding
< Set-Cookie: imooc_uuid=373f05d8-21b6-4c34-af8a-5c3c452cbece; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: imooc_isnew=1; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: imooc_isnew_ct=1516628310; expires=Tue, 22-Jan-2019 13:38:30 GMT; path=/; domain=.imooc.com
< Set-Cookie: cvde=5a65e956d1de1-1; path=/; domain=.imooc.com
< 
{ [data not shown]
100 75022    0 75022    0     0  67577      0 --:--:--  0:00:01 --:--:-- 67648
* Connection #0 to host coding.imooc.com left intact

大于号部分是请求部分,小于号部分是响应部分

2.Nginx日志类型

包括:error.log access_log
error.log 主要是处理http请求错误和nginx本身服务错误状态,按照不同的错误级别记录
access_log 主要是记录处理每次http请求访问的状态
日志主要实现方式是使用 log_format
nginx记录的每次信息都可以当做一个变量,log_format就是将这些变量组合起来,记录到日志中去
我们看一下log_format的配置

Syntax: log_format name [escape=default|json] string …;
Default: log_format combined "...";
Context: http (约束log_format的配置位置)

我们查看一个配置实例,nginx.conf配置文件中
图片.png

Nginx大致有三类变量能够记录在log_format 中

  • HTTP请求变量- arg_PARAMETER http_HEADER send_http_HEADER(服务端返回)
  • 内置变量- Nginx内置的
  • 自定义变量- 自己定义的

例子一:获取到request的请求头信息
首先我们编辑配置文件log_format 部分
在这里插入图片描述
在保存编辑之后检查配置文件的正确性

[root@hongshaorou nginx]# nginx -t -c /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

-t 表示检查正确性
-c 执行目录

检查完成后重新加载服务

[root@hongshaorou nginx]# nginx -s reload -c /etc/nginx/nginx.conf 

然后我们多次请求本机,查看日志输出

[root@hongshaorou nginx]# curl 127.0.0.1

[root@hongshaorou nginx]# tail -n 2 /var/log/nginx/access.log
curl/7.29.0127.0.0.1 - - [24/Jan/2018:20:36:45 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
curl/7.29.0127.0.0.1 - - [24/Jan/2018:20:36:51 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

我们看到已经记录了anget信息。
对于nginx的内置变量,我们可以查看官网信息:
https://nginx.org/en/docs/http/ngx_http_core_module.html#var_status
我们看一下默认的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:会记录每一级请求中信息

作者:海贼之路飞
链接:https://www.jianshu.com/p/d1c7429493b0
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zjcjava/article/details/99232483
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 10:49:54
  • 阅读 ( 1302 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢