Nginx日志类型
nginx的日志有两种,error_log是错误日志,access_log是使用日志
1.error_log: nginx处理http请求错误状态,以及nginx服务本身运行错误的状态,以不同错误级别记录到error_log内.
Syntax: 语法:error_log file [level];
Default: 违约:error_log logs/error.log error;
Context: 背景:main, http, mail, stream, server, location
Configures logging. Several logs can be specified on the same level (1.5.2). If on the main configuration level writing a log to a file is not explicitly defined, the default file will be used.
配置日志记录。 可以在同一级别(1.5.2)上指定多个日志。 如果在主配置级别上未显式定义将日志写入文件,则将使用默认文件。
The first parameter defines a file that will store the log. The special value stderr selects the standard error file. Logging to syslog can be configured by specifying the “syslog:” prefix. Logging to a cyclic memory buffer can be configured by specifying the “memory:” prefix and buffer size, and is generally used for debugging (1.7.11).
第一个参数定义一个将存储日志的文件。 特殊值 stderr 选择标准错误文件。 可以通过指定"syslog:"前缀来配置对 syslog 的日志记录。 可以通过指定"memory:"前缀和缓冲区大小来配置对循环内存缓冲区的日志记录,通常用于调试(1.7.11)。
The second parameter determines the level of logging, and can be one of the following: debug, info, notice, warn, error, crit, alert, or emerg. Log levels above are listed in the order of increasing severity. Setting a certain log level will cause all messages of the specified and more severe log levels to be logged. For example, the default level error will cause error, crit, alert, and emerg messages to be logged. If this parameter is omitted then error is used.
第二个参数决定日志记录的级别,可以是以下参数之一: debug、 info、 notice、 warn、 error、 crit、 alert 或 emerg。 上面的日志级别是按严重性增加的顺序列出的。 设置某个日志级别将导致记录指定的和更严重的日志级别的所有消息。 例如,默认级别错误将导致记录错误、 crit、 alert 和 emerg 消息。 如果省略此参数,则使用错误。
Nginx官网文档地址:http://nginx.org/en//docs/ngx_core_module.html#error_log
2.access_log: 记录nginx每一次的http请求的访问状态,主要用于去分析每一次http的请求和客户端的交互以及对行为的一些分析
代码示例:
access_log /var/log/nginx/access.log main;
语法:
Syntax: 语法:access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: 违约:access_log logs/access.log combined;
Context: 背景:http, server, location, if in location, limit_except
Nginx官网文档地址:http://nginx.org/en//docs/http/ngx_http_log_module.html#access_log
备注:nginx的日志主要依赖于log_format的配置,nginx的日志里面记录的信息可以理解为就是log_format,但是log_format又是一个一个的变量拼接起来的,所以下面来看看log_format
log_format配置
官网对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;
语法分析:
Syntax:(语法:)log_format name [escape=default|json|none] string ...;
备注:结合上面的使用示例 ,语法中的 name=main , 然后 [escape=default|json|none]这些参数都没有填 , 语法中的 string就是后面所有的字符串了 , 示例中string 的值全是不同的变量拼接的一个字符串 , 所以下面还得接着看看Nginx变量的介绍,
Default:(缺省值:)log_format combined "...";
Context:(环境:)http
备注:对于log_format配置的位置是有约束的 , 这里Context写明了http , 那log_format就只能配置在http模块中,不要配置在server中了,server属于http模块中的子模块了
Nginx官网文档地址:http://nginx.org/en//docs/http/ngx_http_log_module.html#log_format
Nginx变量
nginx的变量主要分三种:
http内核模块提供的变量 :
参数名称 注释
$arg_PARAMETER HTTP 请求中某个参数的值,如/index.php?site=www.ttlsa.com,可以用$arg_site 取得www.ttlsa.com这个值.
$args HTTP 请求中的完整参数。例如,在请求/index.php?width=400&height=200 中,$args表示字符串width=400&height=200.
$binary_remote_addr 二进制格式的客户端地址。例如:\\x0A\\xE0B\\x0E
$body_bytes_sent 表示在向客户端发送的http响应中,包体部分的字节数
$content_length 表示客户端请求头部中的Content-Length 字段
$content_type 表示客户端请求头部中的Content-Type 字段
$cookie_COOKIE 表示在客户端请求头部中的cookie 字段
$document_root 表示当前请求所使用的root 配置项的值
$uri 表示当前请求的URI,不带任何参数
$document_uri 与$uri 含义相同
$request_uri 表示客户端发来的原始请求URI,带完整的参数。
$uri和$document_uri未必是用户的原始请求,在内部重定向后可能是重定向后的URI,而$request_uri 永远不会改变,始终是客户端的原始URI.
$host 表示客户端请求头部中的Host字段。如果Host字段不存在,则以实际处理的server(虚拟主机)名称代替。如果Host字段中带有端口,如IP:PORT,那么$host是去掉端口的,它的值为IP。
$host 是全小写的。这些特性与http_HEADER中的http_host不同,http_host只取出Host头部对应的值。
$hostname 表示 Nginx所在机器的名称,与 gethostbyname调用返回的值相同
$http_HEADER 表示当前 HTTP请求中相应头部的值。HEADER名称全小写。例如,示请求中 Host头部对应的值 用 $http_host表
$sent_http_HEADER 表示返回客户端的 HTTP响应中相应头部的值。HEADER名称全小写。例如,用 $sent_ http_content_type表示响应中 Content-Type头部对应的值
$is_args 表示请求中的 URI是否带参数,如果带参数,$is_args值为 ?,如果不带参数,则是空字符串
$limit_rate 表示当前连接的限速是多少,0表示无限速
$nginx_version 表示当前 Nginx的版本号
$query_string 请求 URI中的参数,与 $args相同,然而 $query_string是只读的不会改变
$remote_addr 表示客户端的地址
$remote_port 表示客户端连接使用的端口
$remote_user 表示使用 Auth Basic Module时定义的用户名
$request_filename 表示用户请求中的 URI经过 root或 alias转换后的文件路径
$request_body 表示 HTTP请求中的包体,该参数只在 proxy_pass或 fastcgi_pass中有意义
$request_body_file 表示 HTTP请求中的包体存储的临时文件名
$request_completion 当请求已经全部完成时,其值为 “ok”。若没有完成,就要返回客户端,则其值为空字符串;或者在断点续传等情况下使用 HTTP range访问的并不是文件的最后一块,那么其值也是空字符串。
$request_method 表示 HTTP请求的方法名,如 GET、PUT、POST等
$scheme 表示 HTTP scheme,如在请求 https://nginx.com/中表示 https
$server_addr 表示服务器地址
$server_name 表示服务器名称
$server_port 表示服务器端口
$server_protocol 表示服务器向客户端发送响应的协议,如 HTTP/1.1或 HTTP/1.0
内置变量 : 可以打开Nginx官方文档http://nginx.org/en//docs/http/ngx_http_log_module.html#log_format查看
$bytes_sent 发送的字节数的客户端
$connection 连接序号
$connection_requests 在当前经由一个连接 (1.1. 18)
$msec 时间在毫秒和秒的分辨率在写日志
$pipe “p如果请求是 “流水线”.“否则
$request_length 请求 ( 包含请求行、报头和请求主体)
$request_time 请求处理时间 (以秒和毫秒的分辨率 ;经过的时间之间的第一字节读取到客户端和服务器的日志写入的最后一个字节后被传送至客户端
$status 状态响应
$time_iso8601 当地时间在 ISO 8601 格式标准
$time_local 在当地时间的公共日志格式
自定义变量 : 自己定义