Nginx使用TCP反向代理日志配置不同于http

  修改nginx配置文档/usr/local/nginx/conf/nginx.conf 设置日志格式

stream {
   log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    include ./conf.d/*.tcpstream;
}

  修改具体的tcp配置文档

server{
    error_log       /opt/log/device5001_error.log;
    access_log      /opt/log/device5001_access.log proxy;
    listen 5001;
    proxy_pass device5001;
}

  检查日志

 tail -f /opt/log/device5001_access.log

nginx启用TCP反向代理日志配置_字节数

  PS

•测试发现nginx会等待session结束才会记录到日志文件;
•session日志只是tcp层面的记录,包括session时间,发送接收字节数等等;
•session内部发送日志(比如一个socket连接建立起来以后,多次发送心跳数据)需要在应用层面才能记录;