AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 837703
Accepted
Tim
Tim
Asked: 2017-03-12 11:09:28 +0800 CST2017-03-12 11:09:28 +0800 CST 2017-03-12 11:09:28 +0800 CST

AWStats 日志解析 - 自定义 Nginx 日志格式

  • 772

我已安装 AWStats 7.0(Amazon Linux 存储库中的最新版本)以尝试获取有关带宽使用情况的更多信息。我无法让 AWSStats 解析我的日志 - 我怀疑这是因为我无法正确获取 LogFormat。

我尝试了很多变化,但我无法让它工作。

这是我的 Nginx 日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$host" "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" "$request_time" '
                  '"$upstream_cache_status" "$sent_http_content_encoding" ';

这是一个日志条目

1.1.1.1 - - [12/Mar/2017:07:23:53 +1300] "www.example.com" "GET /url/ HTTP/1.1" 200 7455 "https://www.google.ru/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "46.71.136.54" "0.000" "HIT" "gzip"

这是我的 AStats 配置文件。此处没有的任何内容都是标准的,并且从主配置文件继承

# Path to you nginx vhost log file
LogFile="/var/log/nginx/pts.access.log"

# Domain of your vhost
SiteDomain="example.com"

# Directory where to store the awstats data
DirData="/var/lib/awstats/pts/"

# Other alias, basically other domain/subdomain that's the same as the domain above
HostAliases="www.example.com"

LogFormat = "%host %logname %time1 %virtualname %methodurl %code %bytesd %refererquot %uaquot %otherquot %otherquot %otherquot %otherquot"

这是awstats的输出

[root]# /usr/share/awstats/tools/awstats_updateall.pl now -awstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl
Running '"/usr/share/awstats/wwwroot/cgi-bin/awstats.pl" -update -config=example.com -configdir="/etc/awstats"' to update config example.com
Create/Update database for config "/etc/awstats/awstats.example.com.conf" by AWStats version 7.0 (build 1.971)
From data in log file "/var/log/nginx/pts.access.log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Jumped lines in file: 0
Parsed lines in file: 323
 Found 323 dropped records,
 Found 0 comments,
 Found 0 blank records,
 Found 0 corrupted records,
 Found 0 old records,
 Found 0 new qualified records.

任何人都可以发现不正确的地方吗?我找不到任何可以提供更多信息的附加信息或 awstats 日志。

nginx awstats
  • 2 2 个回答
  • 3112 Views

2 个回答

  • Voted
  1. Tero Kilkanen
    2017-03-13T05:03:41+08:002017-03-13T05:03:41+08:00

    一个可能的问题在这里:

    log_format  main  '$remote_addr - $remote_user [$time_local]...
    

    AStats中对应的配置:

    LogFormat = "%host %logname %time1
    

    您的日志文件包含:

    1.1.1.1 - - [12/Mar/2017:07:23:53 +1300]
    

    %logname仅匹配单个字符串,即 HTTP 身份验证中提供的用户名。现在,您的日志文件包含两个破折号,第一个来自您的配置,第二个表示空用户名。

    因此,AWStats 尝试将第二个破折号解释为时间戳,这导致它认为记录失败。

    因此,您要么需要将破折号添加到 AWStats 日志格​​式字符串,要么从 nginx 日志格式中删除破折号。

    作为旁注,您不需要在 nginx 日志中引用最后一个参数 ( $request_time, $upstream_cache_status, $sent_http_content_encoding),因为它们不能包含空格。

    %extraX如果您想在基于这些事实的报告中使用该信息,您也可以在 AStats 配置中使用。

    • 0
  2. Best Answer
    Tim
    2017-03-17T12:25:33+08:002017-03-17T12:25:33+08:00

    经过大约6个小时的努力,我终于解决了。他们的关键问题是我的 AWStats 站点配置不正确,但我认为我的 Nginx 日志格式或我的 AWStats 格式字符串也不正确。

    这是我的工作 Nginx 日志格式。这是标准的 Nginx 组合日志格式,映射到 awstats LogFormat=1,加上我在日志中想要的三个额外字段

    # /etc/nginx/nginx.conf
    log_format combined_custom '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" $host $request_time $upstream_cache_status';
    

    当然,我必须让我的服务器使用这个配置。这是在我的服务器块中。

    # /etc/nginx/sites-enabled/example.com.conf
    access_log  /var/log/nginx/access.log combined_custom;
    

    这是我的 AWStats 站点配置文件。这会使用站点特定的值扩展 /etc/awstats/awstats.conf.local 文件。

    请注意,一个问题是我的 SiteDomain 错误 - 我在域的开头省略了“www”。我这样做的原因是因为我认为“HostAliases”可以让我将 www 子域添加为别名,但这不是它的用途。这是为了

    该参数[HostAliases]用于分析日志文件中的referer字段,帮助AWStats判断一个referer URL是同一个站点的本地URL还是另一个站点的URL。

    # /etc/awstats/awstats.example.com.conf
    # Path to you nginx vhost log file
    LogFile="/var/log/nginx/access.log"
    
    # Domain of your vhost
    SiteDomain="www.example.com"
    
    # Directory where to store the awstats data
    DirData="/var/lib/awstats/example/"
    
    # Other alias, basically other domain/subdomain that's the same as the domain above
    HostAliases="localhost"
    
    # Performance optimisation
    DNSLookup=0
    
    # This works with the Nginx combined log format
    # LogFormat=1
    
    # This is the equivalent of LogFormat=1
    # LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot"
    
    # This adds my custom fields
    LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %virtualname %other %other"
    

    我还没有进一步让 AWSStats 工作,但一旦我这样做了,我会用我觉得棘手的任何东西来更新这篇文章。

    感谢@Tero Kilkanen 提供了解决此问题的方法 - 即从组合格式开始并向前推进。

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve