• nginx日志输出配置json格式


    修改nginx配置文件

    http {
        include       mime.types;
        default_type  application/octet-stream;
        charset  utf-8;
        
        # 原有日志格式
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" $request_time';
        # json日志格式
        log_format log_json '{"@timestamp": "$time_local", '
                            '"remote_addr": "$remote_addr", '
                            '"referer": "$http_referer", '
                            '"request": "$request", '
                            '"status": $status, '
                            '"bytes": $body_bytes_sent, '
                            '"agent": "$http_user_agent", '
                            '"x_forwarded": "$http_x_forwarded_for", '
                            '"up_addr": "$upstream_addr",'
                            '"up_host": "$upstream_http_host",'
                            '"up_resp_time": "$upstream_response_time",'
                            '"request_time": "$request_time"'
                            ' }';
    
        access_log  logs/access.log log_json; # 引用日志格式名称
    
        (省略内容)
    }
    

    在 Nginx 的配置文件nginx.conf中,我们定义了两种的日志格式:main和log_json,其中,main为普通的文本格式,log_json为 json 格式。log_json其实就是手工构造一个 json 字符串。定义了 json 的日志格式后,便可以指定 access log 为 json 格式.
    修改 Nginx 的配置,重启 Nginx ,便可以看到 json 格式的日志,重启 Nginx:

    扩展:log_format指令中常用的一些变量
    地址:https://www.cnblogs.com/sanduzxcvbnm/p/14250758.html

  • 相关阅读:
    软件项目管理阅读笔记02
    大二下周总结(11)
    “帮你APP”团队冲刺9
    “帮你APP”团队冲刺8
    “帮你APP”团队冲刺7
    “帮你APP”团队冲刺6
    “帮你APP”团队冲刺5
    单词统计
    “帮你APP”团队冲刺4
    “帮你APP”团队冲刺3
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/14251030.html
Copyright © 2020-2023  润新知