目录

一、nginx的日志类型

  • nginx日志包括error.log和access.log ;
  • error.log主要记录nginx处理http请求的错误状态以及nginx本身错误服务的运行状态;
  • access.log 主要记录nginx处理http请求的访问状态,主要用于分析每次的访问请求、和客户端的交互以及对行为的一些分析;

二、nginx的日志位置

  • 查看nginx配置文件,可在配置文件中找到nginx日志位置
[root@localhost /]# cat /etc/nginx/nginx.conf

在这里插入图片描述

  • 如上图所示,error.log和access.log位于 /var/log/nginx目录下。

三、nginx的日志内容查看

  • 输入如下命令查看error.log日志内容
[root@localhost /]# cat /var/log/nginx/error.log

在这里插入图片描述

  • 输入如下命令查看access.log日志内容
[root@localhost /]# cat /var/log/nginx/access.log

在这里插入图片描述

四、access.log是如何实现的

  • access.log主要依赖于log\_format的配置,如下图,在nginx配置文件的http块部分定义了log\_format
    在这里插入图片描述

五、log\_format的详解

1、log\_format语法


#log_format 表示关键字;
#name 表示格式的名字
#[escape=default|json] string ... 表示所有的变量或者字符串
log_format  name [escape=default|json] string ...;

2、log\_format配置位置

  • log\_format只能配置在htpp块当中

在这里插入图片描述

3、配置文件中log\_format 关键字后main格式的名字解释

  • log\_format 关键字后的main表示日志输出格式的名称
  • access\_log /var/log/nginx/access.log后的main表示以log\_format 关键字后的main格式输出日志,如下图:
    在这里插入图片描述

4、配置文件中main格式名字后的一串字符串的解释

  • main格式名字后的一串字符串都是由美元符号和字符串组成的变量,所有变量通过一定的格式组合在一起形成日志输出格式。
    在这里插入图片描述

六、nginx的日志变量有哪些

nginx的日志变量包括http请求变量、nginx内置变量、自定义变量

1、HTTP请求变量

  • 请求变量格式
arg_PARAMETER 表示请求参数变量
http_HEADER 表示http请求的header
send_http_HEADER 表示服务端返回给客户端的response的header

2、HTTP请求变量配置到日志变量示例

以http请求中header部分的 User-Agent参数演示,输出到access.log中
  • 查看http请求头部分的User-Agent参数, 即User-Agent: curl/7.29.0
[root@localhost /]# curl -v www.baidu.com >dev/null

在这里插入图片描述

  • 编辑nginx配置文件,#并在nginx配置文件中log\_format main后追加’$http\_user\_agent’ 变量,保存退出
#编辑nginx配置文件
[root@localhost /]# vim /etc/nginx/nginx.conf
#并在nginx配置文件中log_format  main后追加'$http_user_agent' 变量
#'$http_user_agent'变量以美元符号开头,并以http的request请求头部变量组成
log_format  main '$http_user_agent'  '$remote_addr 

在这里插入图片描述

  • 检查配置文件是否正确,输出 successful表示语法无错误
#检查配置文件是否正确,输出 successful表示语法无错误
[root@localhost /]# vim /etc/nginx/nginx.conf

在这里插入图片描述

  • 重新加载配置文件
#重新加载配置文件
[root@localhost /]# nginx -s reload -c /etc/nginx/nginx.conf
[root@localhost /]#

在这里插入图片描述

  • 请求本机nginx页面
#请求本机nginx页面
[root@localhost /]# curl http://127.0.0.1

在这里插入图片描述

  • 查看access.log日志输出格式,会在输出的每一行最前面看到curl/7.29.01
#查看access.log日志输出格式,会在输出的每一行最前面看到curl/7.29.01
[root@localhost /]# tail -n 200 var/log/nginx/access.log

在这里插入图片描述

3、内置变量

在这里插入图片描述

在这里插入图片描述

  • 找到Syntax: log\_format name [escape=default|json|none] string …;这部分,即时nginx的内置变量,链接地址

在这里插入图片描述

  • 在往下找到$status链接,即可查看所有nginx内置变量,链接地址
    在这里插入图片描述
    在这里插入图片描述

4、自定义变量

  • 暂时无

标签: nginx, 变量, 日志, Nginx, Centos7, http, log, format

相关文章推荐

添加新评论,含*的栏目为必填