1. 程式人生 > >nginx啟用TCP反向代理日誌配置

nginx啟用TCP反向代理日誌配置

不同 server 技術 結束 bubuko proxy ima time 等待

  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

技術分享圖片

  PS

?測試發現nginx會等待session結束才會記錄到日誌文件;
?session日誌只是tcp層面的記錄,包括session時間,發送接收字節數等等;
?session內部發送日誌(比如一個socket連接建立起來以後,多次發送心跳數據)需要在應用層面才能記錄;

  

nginx啟用TCP反向代理日誌配置