• 【2022】Nginx使用ngx_http_log_module模块定义日志


    ngx_http_log_module

    该模块用于定义nginx日志模式

    官方示例:

    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    
    access_log /spool/logs/nginx-access.log compression buffer=32k;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用log_format定义日志格式:

    Syntax:	log_format name [escape=default|json|none] string ...;
    Default:	log_format combined "...";
    Context:	http
    
    • 1
    • 2
    • 3

    格式中常用变量:

    remote_addr:记录客户端ip
    remote_user:记录和客户端用户名
    time_local:记录通用本地时间
    time_lso8601:符合ISO8601标准的通用本地时间
    request:记录请求的方法和协议
    status:记录请求状态码
    body_bytes_sent:发送给客户端的资源字节数,不包括请求头
    bytes_sent:发送给客户端的总字节数
    msec:日志写入时间,单位是秒,精确是毫秒
    http_referer:记录从哪个链接页面访问过来的
    http_user_agant:记录客户端浏览器相关信息
    http_x_forwarderd_for:记录客户端真实IP地址
    request_length:请求的长度
    request_time:请求花费的时间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    nginx变量记录在http://nginx.org/en/docs/varindex.html中,如果有更多的需求可以到这里查找。

    access_log日志定义

    语法:日志可以定义在http、server、location等层,一般放在server层就可以

    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
    
    • 1
    • 2
    • 3
    • 4
    • 一个例子:
    log_format apm '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $bytes_sent '
                    '"$http_referer" "$http_user_agent" "$gzip_ratio"';
    
    
    server {
    
    	listen 80;
    	server_name gzip.yyang.com;
    	root /data/gzip;
    
    	access_log /data/gzip_accrss.log apm;
    
    	location / {
    		index index.html index.htm;		
    	}
    
    ... ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 用浏览器访问网站,在查看访问日志
    192.168.10.1 - - [27/Jul/2022:20:48:07 +0800] "GET /log.txt HTTP/1.1" 200 7737 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36" "42.95"
    
    • 1

    此为访问一次网站的访问记录。

    • 日志过滤
      比如访问某无关紧要的内容不想写入日志中,可以这样:
    location /favicon.ico {
    	access_log off;
    	expires 365d;
    }
    
    • 1
    • 2
    • 3
    • 4

    error.log错误日志

    一般error日志,配置的日志界别越低,产生的日志内容就越丰富,占用的存储空间也就越大,同时频繁的磁盘i/o与网络i/o也会影响nginx性能。
    测试环境可以设置到debug级别;业务刚上线可以是info级别,稳定可以更新为warn级别。

    • nginx错误日志一般有一下几个级别:

      debug、info、notice、warn、error、crit、alert、emerg

    • 配置示例:默认配置

    error_log /var/log/nginx/error_log warn;
    
    关闭错误日志
    error_log /dev/null;
    
    • 1
    • 2
    • 3
    • 4

    nginx日志本地保存管理

    以access日志来说,每一万个访问日志大概占用1MB的磁盘,那么当有大量的请求的时候就会占用很大的空间,处理起来也会不方便。

    我们可以用linux的日志文本管理工具:logrotate管理nginx日志。

    我们可以在/etc/logrotate.d目录下看一下默认的nginx管理方式

    [root@centos7 logrotate.d]# cat nginx 
    /var/log/nginx/*.log {    #日志路径
            daily			  #处理周期
            missingok
            rotate 52		  #回滚周期
            compress		  #配置文件压缩
            delaycompress
            notifempty
            create 640 nginx adm
            sharedscripts
            postrotate		  #文件切割执行脚本
                    if [ -f /var/run/nginx.pid ]; then
                            kill -USR1 `cat /var/run/nginx.pid`
                    fi
            endscript
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    以上内容是每天会对nginx日志进行一次归档,然后进行压缩后保存。周期为52天,从第53天开始就会覆盖第一天的日志,确保本地日志不会无限增长。
    里面的参数可以根据自己需求修改。

    在这里插入图片描述
    大概就是以上截图这个样式。

    使用json方式定义日志

    另外nginx的日志格式可以写成json格式,方便作用于ELK日志平台,就不再这说了,大概写法为:

    log_format log_json ‘{
    	' “remote_addr": "$remote_addr", '
    	' "referer": "$http_referer", '
    };
    
    • 1
    • 2
    • 3
    • 4

    定义多少变量就写多少!

  • 相关阅读:
    LINUX下看门狗的使用
    【Linux网络编程】多进程编程
    Python实验报告——第4章 序列的应用
    关于反逻辑负负得正arr
    你知道企业到什么阶段必须数字化转型吗?
    MyBatisPlus(六)字段映射 @TableField
    Docker 数据管理和网络通信
    Unity接入北斗探针SDK(基于UnityPlayerActivity)丨二、usb-serial-for-android导出jar包
    Vue.js入门教程(四)
    【React的组件&组件间的通信(父向子,子向父,同级组件通信)】
  • 原文地址:https://blog.csdn.net/qq_42527269/article/details/126020753