Nginx log阶段 http_log_module记录access日志

http_log_module


http_log_module这个模块是用来记录access日志的,该模块将HTTP请求的相关信息记录到日志。
 
Nginx日志对于统计、系统服务排错很有用。Nginx日志主要分为两种:access_log(访问日志)和error_log(错误日志)。通过访问日志我们可以得到用户的IP地址、浏览器的信息,请求的处理时间等信息。错误日志记录了访问出错的信息,可以帮助我们定位错误的原因。

 

访问日志主要记录客户端的请求。客户端向Nginx服务器发起的每一次请求都记录在这里。客户端IP,浏览器信息,referer,请求处理时间,请求URL等都可以在访问日志中得到。当然具体要记录哪些信息,你可以通过log_format指令定义。

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

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

 

日志文件格式


日志的格式通过log_format指令来定义的。log_format只能在http指令块中定义。

Syntax: log_format name [escape=default|json|none] string ...;
Default: log_format combined "..."; 默认值是combined,即nginx默认为我们提供的格式
Context: http
combined
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent ' '"$http_referer"
"$http_user_agent"';

定义日志格式为json:

log_format json '{"@timestamp":"$time_iso8601",'
'"status":"$status",'
'"client_ip":"$remote_addr",'
'"method":"$request_method",'
'"size":$body_bytes_sent,'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"request_uri":"$request_uri",'
'"xff":"$http_x_forwarded_for",'
'"referrer":"$http_referer",'
'"agent":"$http_user_agent"}';

server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  json;
}

[root@www ~]# tail -f /usr/local/nginx/conf/nginx.conf  --json日志格式,可以看到json格式内容相对于默认的日志格式main更加具体

{"@timestamp":"2020-05-10T17:10:38+08:00","status":"304","client_ip":"192.168.179.4","method":"GET","size":0,"upstreamhost":"-",
"http_host":"192.168.179.99","request_uri":"/","xff":"-","referrer":"-",
"agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36"}

 

日志文件路径


在定义好日志格式之后,可以定义日志的路径

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

path 指定日志的存放位置。

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

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

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

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

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

(1)path路径可以包含变量:不打开cache时候每次记录一条日志都需要打开关闭日志文件

(2)if通过变量值控制请求,请求日志是否记录

(3)日志缓存

功能:当我们每次记录一条日志,可能这条日志大小并不大,但是引发了一个I/O调用。如果积累一批日志,再批量将内存中的日志写入磁盘,这样就大大的减少了I/O请求,提升性能

写入磁盘条件:1.所有待写入磁盘日志大小超出缓存大小  2.达到flush指定的过期时间  3.Worker进程执行reopen命令  4.或者正在关闭

(4)日志压缩

功能:批量压缩日志中的日志,再写入磁盘,压缩级别默认为1(1压缩率最低,9压缩率最高)

 

日志文件包含变量的优化


the file is opened and closed for each log write. However, since the descriptors of frequently used files can be stored in a cache, writing to the old file can continue during the time specified by the open_log_file_cache directive’s valid parameter 

当我们的access_log path含有变量的时候,为了能够优化,而不需要每一次打开关闭日志文件,可以使用open_log_file_cache来打开缓存,来对经常使用含有变量的日志文件不会被经常打开关闭。(对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:)

Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Default: open_log_file_cache off;
Context: http, server, location
 
max: 
sets the maximum number of descriptors in a cache; if the cache becomes full the least recently used (LRU) descriptors are closed
最大字节数量,缓存内最大句柄数,超过后用LRU算法淘汰

inactive:
sets the time after which the cached descriptor is closed if there were no access during this time; by default, 10 seconds
设置时间,默认是10s,文件访问完后这段时间内不会被关闭

min_uses:sets the minimum number of file uses during the time defined by the inactive parameter to let the descriptor stay open in a cache; by default, 1
日志写入指定次数后压缩

valid:sets the time after which it should be checked that the file still exists with the same name; by default, 60 seconds
设置检查频率,默认60s

off:禁用缓存

Usage example:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
access_log path  [存储路径] [buff=大小] [gzip=压缩级别] [flush=time 刷新时间]
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

 
1. 全局定义  log_format 日志格式
 log_format test '$remote_addr - $remote_user [$time_local]'
                    '"$request" $status $body_bytes_sent'
                    '"$http_referer" "$http_user_agent"';

2. http server配置 
 access_log /application/nginx/logs/access.log test buffer=64k gzip flush=1m;
 open_log_file_cache max=1000 inactive=20s valid=1m min_uses=1;


解释:
buffer=64k   #日志文件缓存在内存中的大小
flush=1m     #内存中日志存留一分钟
buffer满64k才刷盘,日志写入前启用gzip进行压缩,压缩比使用默认值1。缓存数据有效时间为1分钟。假如buffer不满,1分钟强制刷盘


max=1000      #文件描述符
inactive=20s  #日志文件在缓存中没有被使用就会被取消   
valid=1m      #默认 1m 或 60s  两个单位都可以使用
min_uses=1    #在存活时间内日志被写入几次才会记录到缓存
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  logs/access.log  main  buffer=32k flush=1m;
    open_log_file_cache max=1000 inactive=20s valid=1m min_uses=1;



#客户端连续访问三次nginx,然后查看nginx日志,日志记录并没有马上刷新,而是一分钟后刷新在界面
[root@localhost proc]# curl 192.168.179.101
[root@localhost proc]# curl 192.168.179.101
[root@localhost proc]# curl 192.168.179.101

[root@localhost logs]# tail -f access.log 
192.168.179.99 - - [16/Aug/2020:23:22:37 -0400] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
192.168.179.99 - - [16/Aug/2020:23:22:38 -0400] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
192.168.179.99 - - [16/Aug/2020:23:22:39 -0400] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值