zoukankan      html  css  js  c++  java
  • nginx 日志log_format格式

    官方文档:

    http://nginx.org/en/docs/http/ngx_http_log_module.html

    The ngx_http_log_module module writes request logs in the specified format.

    Requests are logged in the context of a location where processing ends. It may be different from the original location, if an internal redirect happens during request processing.

    Example Configuration

    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;

    Nginx大致有三类变量能够记录在log_format 中

    • HTTP请求变量- arg_PARAMETER http_HEADER send_http_HEADER(服务端返回)
    • 内置变量- Nginx内置的
    • 自定义变量- 自己定义的

    我们看一下默认的log_format

        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  /var/log/nginx/access.log  main;
    

    我们看到默认的格式,基本都是单引号 '' 包裹着一些变量,还包括 中划线 - 方括号 [] 作为分隔符一起打印。
    每个变量都有这含义:

    remote_addr:对应客户端的地址
    remote_user:是请求客户端请求认证的用户名,如果没有开启认证模块的话是值为空。
    time_local:表示nginx服务器时间
    request:表示request请求头的行
    status:表示response的返回状态
    body_bytes_sent:表示从服务端返回给客户端的body数据大小
    http_referer:表示请求的上一级页面
    http_user_agent:表示agent信息
    http_x_forwarded_for:会记录每一级请求中信息

    1、log_format 普通格式

    1
    2
    3
    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" "$upstream_response_time"';

    2、log_format JSON 格式

    为了便于利用 Elastic Stack 日志平台收集展示 Nginx 的日志,可以将 Nginx 的日志改成 json 的格式。修改后的 json 日志格式如下所示:

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    log_format logJson '{
                             "@timestamp""$time_local", '
                             '"@fields": { '
                             '"remote_addr": "$remote_addr", '
                             '"remote_user": "$remote_user", '
                             '"body_bytes_sent": "$body_bytes_sent", '
                             '"request_time": "$request_time", '
                             '"status": "$status", '
                             '"request": "$request", '
                             '"request_method": "$request_method", '
                             '"http_referrer": "$http_referer", '
                             '"body_bytes_sent":"$body_bytes_sent", '
                             '"http_x_forwarded_for": "$http_x_forwarded_for", '
                             '"http_user_agent""$http_user_agent" }
                             }';

    nginx的log是没有自动分割功能的。需要自己写shell脚本分割


     
  • 相关阅读:
    JAVA基础:JAVA代码编写的30条建议
    Oracle学习之三 程序控制结构
    ExtJs 常用代码片段(7.30更新)
    ExtJs4 之数据模型
    .NET 大杂烩
    .NET 导出Excel
    Javascript实现IE直接打印
    C# Timer用法及实例
    AjaxPro实现无刷新多级联动实例
    VB6.0输入文本写进UTF8格式的文本文件
  • 原文地址:https://www.cnblogs.com/youxin/p/9746285.html
Copyright © 2011-2022 走看看