Nginx日志

Nginx日志对于统计、系统服务排错很有用。Nginx日志主要分为两种:access_log(访问日志)和error_log(错误日志)。通过访问日志我们可以得到用户的IP地址、浏览器的信息,请求的处理时间等信息。错误日志记录了访问出错的信息,可以帮助我们定位错误的原因。本文将详细描述一下如何配置Nginx日志。

ngx_http_log_module 模块指定日志格式记录请求
log_format NAME string ...;  string可以使用nginx核心模块及其它模块内嵌的变量定义在(上下文:http)
access_log off; #禁用访问日志,访问日志文件路径,格式及相关的缓冲的大小 压缩方式  删除清空日志的时间
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];(上下文:http server)
    path    :日志写入的路径或 指定 syslog: 前缀配置将日志记录到 syslog
    format     指定日志的格式。默认使用预定义的combined。
    buffer     用来指定日志写入时的缓存大小。默认是64k。
    gzip     日志写入前先进行压缩。压缩率可以指定,从1到9数值越大压缩比越高,同时压缩的速度也越慢。默认是1。
    flush     设置缓存的有效时间。如果超过flush指定的时间,缓存中的内容将被清空。
    if         条件判断。如果指定的条件计算为0或空字符串,那么该请求不会写入日志。
    off        如果指定了该值,当前作用域下的所有的请求日志都被关闭



示例

示例
http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';

  log_format zjol '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" ';

server {
  access_log /var/logs/nginx-access.log main buffer=32k;
  access_log /usr/local/nginx/logs/zjol.access.log zjol;
  error_log /usr/local/nginx/logs/zjol.error.log error;



nginx 的默认访问日志记录内容相对比较单一,默认的格式也不方便后期做日志统计分析,
生产环境中通常将nginx日志转换为json日志,然后配合使用ELK做日志收集-统计-分析
log_format zzhz_json '{"@timestamp":"$time_iso8601",'
    '"host":"$server_addr",'
    '"clientip":"$remote_addr",'
    '"size":$body_bytes_sent,'
    '"responsetime":$request_time,'
    '"upstreamtime":"$upstream_response_time",'
    '"upstreamhost":"$upstream_addr",'
    '"http_host":"$host",'
    '"uri":"$uri",'
    '"domain":"$host",'
    '"xff":"$http_x_forwarded_for",'
    '"referer":"$http_referer",'
    '"tcp_xff":"$proxy_protocol_addr",'
    '"http_user_agent":"$http_user_agent",'
    '"status":"$status"}';
access_log /apps/nginx/logs/access_json.log  zzhz_json;

json格式的日志访问统计:
#!/usr/bin/env python 
#coding:utf-8 
#Author:Zhang ShiJie 
status_200= [] 
status_404= [] 
with open("access_json.log") as f: 
    for line in f.readlines(): 
        line = eval(line) 
        if line.get("status") == "200": 
            status_200.append(line.get) 
        elif line.get("status") == "404": 
            status_404.append(line.get) 
        else:
            print("状态码 ERROR") 
f.close() 
 print "状态码200的有--:",len(status_200) 
 print "状态码404的有--:",len(status_404) 
    
测试: [root@ ~]# python nginx_json.py 
状态码200的有--: 1910 
状态码404的有--: 13

将多个日志进行积累,达到一定量级后写入到磁盘,可以减少磁盘旋转,从而降低磁盘i/o,提升nginx能效
缓存各日志文件相关的元数据信息
open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off; 禁用缓存
max:      设置缓存中描述符的最大数量。如果缓存变满,则最近最少使用(LRU)的描述符将被关闭
min_uses:   在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:   如果在此时间后缓存描述符没有被访问,则被关闭,默认为 10 秒
valid:     验证缓存中各缓存项是否为活动项的时间间隔



access_log /application/nginx/logs/access_81.log zjol buffer=64k flush=1m;
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=1;

buffer=64k   #日志文件缓存在内存中的大小
flush=1m     #内存中日志存留一分钟
max=1000     #文件描述符
inactive=20s #日志文件在缓存中没有被使用就会被取消
valid=1m     #默认 1m 或 60s 两个单位都可以使用
min_uses=1   #在存活时间内日志被写入几次才会记录到缓存






清空日志不要用rm,建议使用cat /dev/null  > /log/access_log.log 或 > /log/access_log.log.

 

posted @ 2022-07-16 16:26  yuanbangchen  阅读(1772)  评论(0编辑  收藏  举报