nginx日志格式

最近我们要调用IDL的一个人脸识别接口,但是人家那个接口是部署在南京机房,并且只能内部调用。从最小化成本的考虑,对于海外用户,我们在香港搭建了一个nginx走专线中转进行提速。在国内则是在北京机房。

中转的配置很简单,就是一个简单的proxypass:

http {
    upstream idl {
        server 10.205.xxx.xx:8788;
    }

    upstream idl-test {
        server 10.46.xxx.xx:8787;
    }

    server {
        listen       8080;
        server_name  face.arganzheng.me;

        location /idl/api/v1/recognition {
            rewrite /idl/api/v1/recognition /1 last;
        }

        location /1 {
        proxy_pass http://idl;
        }

        location /idl-test/api/v1/recognition {
            rewrite /idl-test/api/v1/recognition /faceapi last;
        }

        location /faceapi {
        proxy_pass http://idl-test;
    }
}

用户是对 http://face.arganzheng.me/idl/api/v1/recognition 发送 HTTP POST 请求就能够转发给南京的IDL服务。其中POST的内容是一张压缩后的图片的BASE64编码。

用WIFI环境下体验都挺顺畅的,但是在2G或者3G环境下,会发现有时候识别1分钟都没有识别出来。显然问题应该是出现在网络质量上,但是有没有数据支撑呢?Nginx的access_log应该支持吧。查看了一下nginx的log配置说明,果然是有相关的参数的。

对于前端用户请求的信息:Module ngx_http_core_module - Embedded Variables

  • $arg_name
  • $args
  • $binary_remote_addr
  • $body_bytes_sent:number of bytes sent to a client, not counting the response header; this variable is compatible with the “%B” parameter of the mod_log_config Apache module
  • $bytes_sent:number of bytes sent to a client (1.3.8, 1.2.5)
  • $connection
  • $connection_requests
  • $content_length
  • $content_type
  • $cookie_name
  • $document_root
  • $document_uri
  • $host
  • $hostname
  • $http_name
  • $https
  • $is_args
  • $limit_rate
  • $msec
  • $nginx_version
  • $pid
  • $pipe
  • $proxy_protocol_addr
  • $query_string
  • $realpath_root
  • $remote_addr:client address
  • $remote_port
  • $remote_user
  • $request:ull original request line
  • $request_body:不要轻易log这个选项,会打很多日志的。
  • $request_body_file
  • $request_completion
  • $request_filename
  • $request_length:request length (including request line, header, and request body) (1.3.12, 1.2.7)
  • $request_method:request method, usually “GET” or “POST”
  • $request_time:request processing time in seconds with a milliseconds resolution (1.3.9, 1.2.6); time elapsed since the first bytes were read from the client
  • $request_uri
  • $scheme
  • $sent_http_name
  • $server_addr
  • $server_name
  • $server_port
  • $server_protocol
  • $status:response status (1.3.2, 1.2.2)
  • $tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
  • $time_iso8601
  • $time_local:local time in the Common Log Format (1.3.12, 1.2.7)
  • $uri

对于后端upstream的信息: Module ngx_http_upstream_module - Embedded Variables

  • $upstream_addr
  • $upstream_cache_status
  • $upstream_cookie_name
  • $upstream_header_time
  • $upstream_http_name
  • $upstream_response_length
  • $upstream_response_time
  • $upstream_status

还有其他一些变量 Alphabetical index of variables。再结合日志格式说明:Module ngx_http_log_module - log format。于是我们可以这么配置:

log_format  main  '$remote_addr [$time_local] $upstream_response_time - $request_time "$request" $request_length '
                  '$status - $upstream_status $bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" ' ;

另外,因为我们的POST数据比较大,增大buffer的大小可以提到一些性能:

client_max_body_size 10m;
client_body_buffer_size 2m;

超时方面,默认的60s应该足够,这里就不做调整。

最终的nginx配置如下:

#user  nobody;
worker_processes  8;

pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  102400;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr [$time_local] $upstream_response_time - $request_time "$request" $request_length '
                      '$status - $upstream_status $bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" ' ;

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    # Have nginx do the compression.
    gzip  on;
    gzip_comp_level     1;
    gzip_disable        msie6;
    gzip_proxied        any;

    # text/html mime type is automatically included for gzip, have to add the rest
    gzip_types          text/plain text/css application/x-javascript text/xml application/xml application/rss+xml text/javascript;


    # Pass some client identification headers back to the backend_server
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    client_max_body_size 10m;
    client_body_buffer_size 2m;

     upstream idl {
        server 10.205.xxx.xx:8788;
    }

    upstream idl-test {
        server 10.46.xxx.xx:8787;
    }

    server {
        listen       8080;
        server_name  face.arganzheng.me;

        location /idl/api/v1/recognition {
            rewrite /idl/api/v1/recognition /1 last;
        }

        location /1 {
            proxy_pass http://idl;
        }

        location /idl-test/api/v1/recognition {
            rewrite /idl-test/api/v1/recognition /faceapi last;
        }

        location /faceapi {
            proxy_pass http://idl-test;
        }

        location /face/stat/pv.do {
            access_log logs/face_pv.log main;
            proxy_pass http://127.0.0.1:8080/face/stat/postokay;
        }

        location /face/stat/act.do {
            access_log logs/face_act.log main;
            proxy_pass http://127.0.0.1:8080/face/stat/postokay;
        }

        location /face/stat/postokay {
            return 200;
        }

   }
}

注意到这里打点统计还可以指定使用那个log,写到哪个日志文件。

NOTES $request_time 和 $upstream_response_time

需要注意的是,对于HTTP POST的请求,两者相差特别大。因为Nginx会把HTTP request body缓存住,接受完毕后才会把数据一起发给后端。

[Tengine-中文]关于nginx中request_time 和upstream_response_time的疑问

<think>嗯,用户想了解Nginx日志格式的详细解析。首先,我得回忆一下Nginx日志相关的配置。记得日志格式主要是通过log_format指令来定义的,然后在access_log里指定使用的格式日志文件路径。不过用户可能不太清楚具体怎么配置,需要分步骤解释。 首先,应该解释默认的日志格式是什么样的。比如,Nginx默认的combined格式包含哪些变量,比如$remote_addr、$remote_user、$time_local等等。每个变量代表什么,比如$request是用户的请求行,$status是响应状态码。这部分需要详细说明,可能用户需要知道每个字段的含义才能自定义格式。 然后,用户可能想自定义日志格式,所以得举一个例子,比如如何添加特定的变量,比如$http_referer或者$http_user_agent,甚至自定义变量如$upstream_response_time。需要说明log_format的语法结构,比如log_format后面跟格式名称和字符串,字符串里用变量定义各个字段。 接下来,配置访问日志的部分,access_log指令的使用方法。比如指定日志文件路径和使用的格式名称。可能还需要提到日志缓存和gzip压缩的参数,这些优化点用户可能会感兴趣。 错误日志的配置也不能忽略,虽然用户主要问的是日志格式,但error_log的级别和路径设置也是相关的。比如debug级别可以记录更详细的信息,但要注意性能影响。 然后,需要提供一些实际的配置示例,这样用户可以直接参考。比如在nginx.conf里定义main格式,包含常用变量,然后应用到access_log里。同时,说明如何检查日志是否生效,比如用tail命令实时查看日志输出,确保配置正确。 可能还需要提醒用户注意权限问题,比如Nginx进程是否有写入日志文件的权限,避免配置正确但日志无法生成的问题。另外,自定义日志路径后,要记得重新加载配置,比如用nginx -s reload。 用户可能还会关心如何分析这些日志,比如用工具分析访问日志,或者监控错误日志排查问题。不过用户的问题主要是格式解析,这部分可能需要点到为止,或者简单提及。 最后,确保引用了提供的参考内容,比如引用[5]提到log_format和access_log的关系,引用[4]关于变量$status的说明,引用[3]关于日志系统的重要性。这些引用需要自然地融入回答中,帮助用户理解官方文档和最佳实践。 总结一下,回答的结构应该是先介绍日志格式的基本概念,然后详细讲解默认格式和自定义配置,接着是访问日志和错误日志的设置,给出配置示例和验证方法,最后相关问题和引用部分。这样用户能逐步了解如何配置和解析Nginx日志格式,满足他们的需求。</think>### Nginx日志格式详细解析 Nginx日志格式由`log_format`指令定义,并通过`access_log`指令应用到具体的日志文件中。以下为关键解析步骤: --- #### 一、默认日志格式 Nginx默认使用`combined`格式(非显式定义),包含以下核心变量: $$log_format\ combined\ '\$remote_addr - \$remote_user [\$time_local] "\$request" \$status \$body_bytes_sent "\$http_referer" "\$http_user_agent"';$$ - `$remote_addr`:客户端IP地址 - `$time_local`:请求时间(本地时区) - `$request`:完整请求行(如`GET /index.html HTTP/1.1`) - `$status`:HTTP响应状态码(如200、404)[^4] - `$body_bytes_sent`:发送给客户端的字节数(不含响应头) --- #### 二、自定义日志格式 通过`log_format`指令可扩展字段,例如添加响应时间: ```nginx http { log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'rt=$request_time ut=$upstream_response_time'; access_log /var/log/nginx/custom.log main; } ``` - `$request_time`:请求处理总时间(秒) - `$upstream_response_time`:反向代理上游服务器响应时间 --- #### 三、日志配置实践 1. **访问日志路径配置** ```nginx access_log /var/log/nginx/access.log main buffer=32k gzip=1 flush=5m; ``` - `buffer`:启用日志写入缓存(32KB) - `gzip`:启用日志压缩(需Nginx编译时支持) - `flush`:设置缓存刷新时间(5分钟)[^2] 2. **错误日志分级** ```nginx error_log /var/log/nginx/error.log warn; ``` - 日志级别:`debug` > `info` > `notice` > `warn` > `error` > `crit` - 生产环境建议使用`warn`级别平衡信息量与性能[^1] --- #### 四、日志验证与调试 1. 通过命令实时观察日志: ```bash tail -f /var/log/nginx/custom.log ``` 2. 检查日志变量是否生效: ```bash grep 'rt=' /var/log/nginx/custom.log | head -n 3 ``` --- #### 五、高级应用场景 - **安全审计**:通过`$http_x_forwarded_for`记录原始客户端IP(适用于代理环境) - **性能分析**:结合`$upstream_cache_status`分析缓存命中率 - **业务统计**:使用`$arg_*`记录特定URL参数(如`$arg_user_id`) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值