Linux下玩转nginx系列(三)---nginx日志配置文件说明

11 篇文章 0 订阅
10 篇文章 0 订阅
本文详细介绍了Nginx的access_log、error_log日志配置,包括日志路径、格式、缓存设置及其相关参数。还讲解了log_format自定义日志格式,以及如何利用open_log_file_cache提升性能。此外,分享了日志调试技巧,如针对特定IP记录错误、调试rewrite规则和特定URL日志记录。最后,提供了多个日志格式示例和解析,帮助理解不同场景下的日志需求。
摘要由CSDN通过智能技术生成

nginx日志共三个参数。

access_log: 定义日志的路径及格式。

log_format: 定义日志的模板。

open_log_file_cache: 定义日志文件缓存。

nginx access日志配置

access_log日志配置

access_log用来定义日志级别,日志位置。语法如下:
日志级别: debug > info > notice > warn > error > crit > alert > emerg

语法格式: access_log path [format [buffer=size] [gzip[=level]]
[flush=time] [if=condition]];
access_log off;
默认值 : access_log logs/access.log combined;
作用域 : http, server, location, if in
location, limit_except

• path 指定日志的存放位置。

• format 指定日志的格式。默认使用预定义的combined。

• buffer 用来指定日志写入时的缓存大小。默认是64k。

• gzip 日志写入前先进行压缩。压缩率可以指定,从1到9数值越大压缩比越高,同时压缩的速度也越慢。默认是1。

• flush 设置缓存的有效时间。如果超过flush指定的时间,缓存中的内容将被清空。

• if 条件判断。如果指定的条件计算为0或空字符串,那么该请求不会写入日志。

另外,还有一个特殊的值off。如果指定了该值,当前作用域下的所有的请求日志都被关闭。

示例:

基本用法

access_log /var/logs/nginx-access.log

该例子指定日志的写入路径为/var/logs/nginx-access.log,日志格式使用默认的combined。

access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m

该例子指定日志的写入路径为/var/logs/nginx-access.log,日志格式使用默认的combined,指定日志的缓存大小为32k,日志写入前启用gzip进行压缩,压缩比使用默认值1,缓存数据有效时间为1分钟。

log_format 定义日志格式

语法格式: log_format name [escape=default|json] string …;
默认值 : log_format combined “…”;
作用域 : http

• name 格式名称。在access_log指令中引用。

• escape 设置变量中的字符编码方式是json还是default,默认是default。

• string 要定义的日志格式内容。该参数可以有多个。参数中可以使用Nginx变量。

示例:

access_log /var/logs/nginx-access.log main log_format main
‘$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Double superscript at position 11: request"' '̲status
b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer”’ ‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for”’;

我们使用log_format指令定义了一个main的格式,并在access_log指令中引用了它。假如客户端有发起请求:https://suyunfe.com/,我们看一下我截取的一个请求的日志记录:

124.78.32.26 - - [12/Mar/2022:22:18:20 +0800] “GET / HTTP/1.1” 200 569 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51
Safari/537.36” “-”

我们看到最终的日志记录中 r e m o t e u s e r 、 remote_user、 remoteuserhttp_referer、$http_x_forwarded_for都对应了一个-,这是因为这几个变量为空。

设置error_log

错误日志在Nginx中是通过error_log指令实现的。该指令记录服务器和请求处理过程中的错误信息。

  • 语法
    配置错误日志文件的路径和日志级别。

error_log file [level];
Default:
error_log logs/error.log error;

第一个参数指定日志的写入位置。
第二个参数指定日志的级别。level可以是debug, info, notice, warn, error, crit, alert,emerg中的任意值。可以看到其取值范围是按紧急程度从低到高排列的。只有日志的错误级别等于或高于level指定的值才会写入错误日志中。默认值是error。

  • 基本用法

error_log /var/logs/nginx/nginx-error.log

它可以配置在:main, http, mail, stream, server, location作用域。
例子中指定了错误日志的路径为:/var/logs/nginx/nginx-error.log,日志级别使用默认的error。

常见的日志变量

• $remote_addr, $http_x_forwarded_for 记录客户端IP地址。

• $remote_user记录客户端用户名称。

• $request记录请求的URL和HTTP协议(GET,POST,DEL,等)。

• $status记录请求状态。

• $body_bytes_sent发送给客户端的字节数,不包括响应头的大小; 该变量与Apache模块mod_log_config里的“%B”参数兼容。

• $bytes_sent发送给客户端的总字节数。

• $connection连接的序列号。

• $connection_requests 当前通过一个连接获得的请求数量。

• $msec 日志写入时间。单位为秒,精度是毫秒。

• $pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”。

• $http_referer 记录从哪个页面链接访问过来的。

• $http_user_agent记录客户端浏览器相关信息。

• $request_length请求的长度(包括请求行,请求头和请求正文)。

• $request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。

• $time_iso8601 ISO8601标准格式下的本地时间。

• $time_local通用日志格式下的本地时间。

open_log_file_cache

使用open_log_file_cache来设置日志文件缓存(默认是off)。

• max 设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。

• inactive 设置存活时间,默认是10s。

• min_uses 设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次。

• valid 设置检查频率,默认60s。

• off 禁用缓存。

语法格式: open_log_file_cache max=N [inactive=time] [min_uses=N]
[valid=time];
open_log_file_cache off;
默认值: open_log_file_cache off;
作用域: http, server, location

示例

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

它可以配置在http、server、location作用域中。
例子中,设置缓存最多缓存1000个日志文件描述符,20s内如果缓存中的日志文件描述符至少被被访问2次,才不会被缓存关闭。每隔1分钟检查缓存中的文件描述符的文件名是否还存在。

nginx日志调试技巧

设置 Nginx 仅记录来自于指定的 IP 的错误

当设置日志级别成 debug,如果在调试一个在线的高流量网站的话,错误日志可能会记录每个请求的很多消息,这样会变得毫无意义。
在events{…}中配置如下内容,可以使 Nginx 记录仅仅来自于指定的 IP 的错误日志。

events {
debug_connection 1.2.3.4;
}

调试 nginx rewrite 规则

调试rewrite规则时,如果规则写错只会看见一个404页面,可以在配置文件中开启nginx rewrite日志,进行调试。

server {
error_log /var/logs/nginx/example.com.error.log;
rewrite_log on;
}

rewrite_log on; 开启后,它将发送所有的 rewrite 相关的日志信息到 error_log 文件中,使用 [notice] 级别。随后就可以在error_log 查看rewrite信息了。

使用location记录指定URL的日志

server {
error_log /var/logs/nginx/example.com.error.log;
location /static/ {
error_log /var/logs/nginx/static-error.log debug;
}
}

配置以上配置后,/static/ 相关的日志会被单独记录在static-error.log文件中。

常用例子

main格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
access_log  logs/access.log  main;

json格式

log_format logstash_json '{"@timestamp":"$time_iso8601",'
       '"host": "$server_addr",'
       '"client": "$remote_addr",'
       '"size": $body_bytes_sent,'
       '"responsetime": $request_time,'
       '"domain": "$host",'
       '"url":"$request_uri",'
       '"referer": "$http_referer",'
       '"agent": "$http_user_agent",'
       '"status":"$status",'
       '"x_forwarded_for":"$http_x_forwarded_for"}';

解释:

u r i 请 求 中 的 当 前 U R I ( 不 带 请 求 参 数 , 参 数 位 于 uri请求中的当前URI(不带请求参数,参数位于 uriURI(args),不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。
r e q u e s t u r i 这 个 变 量 等 于 包 含 一 些 客 户 端 请 求 参 数 的 原 始 U R I , 它 无 法 修 改 , 请 查 看 request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看 requesturiURIuri更改或重写URI。也就是说: r e q u e s t u r i 是 原 始 请 求 U R L , request_uri是原始请求URL, requesturiURLuri则是经过nginx处理请求后剔除参数的URL,所以会将汉字表现为union。

坑点:

使用 u r i 可 以 在 n g i n x 对 U R L 进 行 更 改 或 重 写 , 但 是 用 于 日 志 输 出 可 以 使 用 uri可以在nginx对URL进行更改或重写,但是用于日志输出可以使用 uringinxURL使request_uri代替,如无特殊业务需求,完全可以替换。

压缩格式

日志中增加了压缩的信息。

http {
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    server {
        gzip on;
        access_log /spool/logs/nginx-access.log compression;
        ...
    }
}

upstream格式

增加upstream消耗的时间。

http {
    log_format upstream_time '$remote_addr - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_referer" "$http_user_agent"'
                             'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

    server {
        access_log /spool/logs/nginx-access.log upstream_time;
        ...
    }
}

总结

Nginx中通过access_log和error_log指令配置访问日志和错误日志,通过log_format我们可以自定义日志格式。如果日志文件路径中使用了变量,我们可以通过open_log_file_cache指令来设置缓存,提升性能。

另外,在access_log和log_format中使用了很多变量,这些变量没有一一列举出来,详细的变量信息可以参考Nginx官方文档

查看日志使用技巧

统计status 出现的次数

awk '{print $9}' access.log | sort | uniq -c | sort -rn

36461 200 
483 500
9 400
3 302
1 403
1 301

显示返回302状态码的URL

awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn

1 /anyrtc.png
1 /test.html

参考文档

Nginx日志配置详解
如何在Nginx中配置自定义访问和错误日志格式
Full Example Configuration

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值