插件窝 干货文章 Nginx访问日志分析配置,实时监控网站访问情况

Nginx访问日志分析配置,实时监控网站访问情况

Nginx 分析 日志 配置 556    来源:    2025-04-17

Nginx访问日志分析与实时监控配置指南

一、Nginx日志配置优化

1. 日志格式配置

nginx.conf中自定义日志格式(推荐JSON格式便于分析):

http {
    log_format json_combined escape=json
    '{"time_local":"$time_local",'
    '"remote_addr":"$remote_addr",'
    '"remote_user":"$remote_user",'
    '"request":"$request",'
    '"status":$status,'
    '"body_bytes_sent":$body_bytes_sent,'
    '"http_referer":"$http_referer",'
    '"http_user_agent":"$http_user_agent",'
    '"http_x_forwarded_for":"$http_x_forwarded_for",'
    '"request_time":$request_time,'
    '"upstream_response_time":"$upstream_response_time"}';

    access_log /var/log/nginx/access.log json_combined;
}

2. 日志轮转配置

/etc/logrotate.d/nginx中配置日志轮转:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        /etc/init.d/nginx reopen-logs >/dev/null 2>&1
    endscript
}

二、实时日志分析方案

1. 使用GoAccess实时分析

安装GoAccess:

# Ubuntu/Debian
sudo apt-get install goaccess

# CentOS/RHEL
sudo yum install goaccess

实时监控命令:

goaccess /var/log/nginx/access.log -o /var/www/html/report.html --real-time-html --log-format=JSON

2. 使用ELK Stack (Elasticsearch + Logstash + Kibana)

Logstash配置示例 (nginx.conf):

input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => "beginning"
    sincedb_path => "/dev/null"
    codec => "json"
  }
}

filter {
  mutate {
    remove_field => ["@version", "host"]
  }
  geoip {
    source => "remote_addr"
    target => "geoip"
  }
  useragent {
    source => "http_user_agent"
    target => "user_agent"
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "nginx-access-%{+YYYY.MM.dd}"
  }
}

3. 使用Grafana + Prometheus + Loki

Loki配置示例:

scrape_configs:
- job_name: nginx
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx-access
      __path__: /var/log/nginx/access.log

三、关键监控指标

  1. 请求率监控:

    • 总请求数/秒
    • 按HTTP方法(GET/POST等)分类的请求数
  2. 响应状态监控:

    • 2xx/3xx/4xx/5xx状态码比例
    • 特定错误码(如404, 500)的请求
  3. 性能监控:

    • 平均响应时间
    • 慢请求(>1s)比例
    • 上游响应时间
  4. 流量监控:

    • 带宽使用情况
    • 按URL/API端点分类的流量
  5. 安全监控:

    • 可疑IP访问
    • 异常User-Agent
    • 暴力破解尝试

四、告警配置示例

1. 使用Prometheus Alertmanager

groups:
- name: nginx-alerts
  rules:
  - alert: HighErrorRate
    expr: sum(rate(nginx_http_requests_total{status=~"5.."}[5m])) by (host) / sum(rate(nginx_http_requests_total[5m])) by (host) > 0.05
    for: 10m
    labels:
      severity: critical
    annotations:
      summary: "High error rate on {{ $labels.host }}"
      description: "5xx error rate is {{ $value }}"

2. 使用Grafana告警

在Grafana面板中设置: - 当5xx错误率>5%持续5分钟时触发告警 - 当平均响应时间>1秒持续10分钟时触发告警

五、高级分析技巧

  1. 用户行为分析:

    • 会话跟踪(通过cookie或IP+User-Agent)
    • 热门访问路径
    • 转化漏斗分析
  2. API性能分析:

    • 按端点分类的响应时间
    • 请求参数与性能关联分析
  3. 地理分布分析:

    • 使用GeoIP模块分析访问者地理位置
    • 按地区的性能差异
  4. 设备分析:

    • 移动端/桌面端比例
    • 浏览器/操作系统分布

六、性能优化建议

  1. 对于高流量网站,考虑:

    • 使用syslog-ng或rsyslog替代直接文件日志
    • 增加日志缓冲区大小(access_log buffer=32k)
    • 在高负载时段暂时关闭访问日志
  2. 日志分析服务器应与生产环境分离,避免影响性能

  3. 考虑使用CDN日志分析作为补充

通过以上配置和分析方案,您可以全面掌握网站的访问情况,及时发现并解决问题,优化用户体验和系统性能。