【问题标题】:how to pipe logfiles to python in nginx如何在 nginx 中将日志文件通过管道传输到 python
【发布时间】:2017-10-31 16:21:23
【问题描述】:

如何在不先将其保存在日志文件中的情况下将 nginx 日志输出通过管道传输到 python 脚本。
目前我使用的是 apache,并像这样通过管道传输它:

CustomLog "|/path/to/script.py"

在 nginx 中,日志文件的配置如下所示,现在我需要将其通过管道传输到 python 脚本

# Nginx access file:
$InputFileName /var/log/nginx/access.log
$InputFileTag nginx-access:
$InputFileStateFile stat-nginx-access
$InputFileSeverity info
$InputFilePersistStateInterval 20000
$InputRunFileMonitor

#Nginx Error file: 
$InputFileName /var/log/nginx/error.log
$InputFileTag nginx-error:
$InputFileStateFile stat-nginx-error
$InputFileSeverity error
$InputFilePersistStateInterval 20000
$InputRunFileMonitor

【问题讨论】:

    标签: apache nginx logging pipe


    【解决方案1】:

    感谢 nginx IRC 频道,我找到了一个合适的解决方案,我想分享给下一个遇到同样问题的人。

    http {
      #(...)
    
    
      map $remote_addr $anon_remote_addr {
        "~^(?<ip_a>\d+\.\d+)\.\d+\.\d+" "$ip_a";
      }
      log_format combined_anon '$anon_remote_addr.0.0 - $remote_user [$time_local] '
                               '"$request" $status $body_bytes_sent '
                               '"$http_referer" "$http_user_agent"';
      # Set it globally
      access_log /var/log/nginx/access.log combined_anon;
    
    
      #(...)
    }
    

    片段来自:https://gist.github.com/fluential/e2b4ceea628ead5162f0

    【讨论】:

    • 明确地说,这是一个匿名日志文件的解决方案,不将日志传送到外部程序(这就是问题所在)。
    猜你喜欢
    • 2019-10-26
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多