You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置Grafana Agent适配Nginx遇阻,求参数填充指引

如何正确配置Grafana Agent的Nginx监控(日志+指标)?

我尝试为Nginx配置Grafana Agent的grafana-agent.yml文件,但多次尝试均未成功。官方提供的包含Nginx相关配置的示例文件如下:

integrations:
  agent:
    enabled: true
    relabel_configs:
    - action: replace
      source_labels:
      - agent_hostname
      target_label: instance
    metric_relabel_configs:
    - action: keep
      regex: (prometheus_target_.*|prometheus_sd_discovered_targets|agent_build.*|agent_wal_samples_appended_total|process_start_time_seconds)
      source_labels:
      - __name__
  prometheus_remote_write:
  - basic_auth:
      password: <your_prom_pass>
      username: <your_prom_user>
    url: <your_prom_url>
logs:
  configs:
  - clients:
    - basic_auth:
        password: <your_loki_pass>
        username: <your_loki_user>
      url: <your_loki_url>
    name: integrations
    positions:
      filename: /tmp/positions.yaml
    scrape_configs:
    - job_name: integrations/nginx
      static_configs:
      - targets:
        - localhost
        labels:
          host: <http_hostname>
          __path__: <path to json nginx access log>
metrics:
  configs:
  - name: integrations
    remote_write:
    - basic_auth:
        password: <your_prom_pass>
        username: <your_prom_user>
      url: <your_prom_url>
    scrape_configs:
  global:
    scrape_interval: 60s
  wal_directory: /tmp/grafana-agent-wal

但我不清楚metrics.config.basic_auth.password、logs.configs.scrape_configs.targets.host等字段应填入的具体信息,也找不到更详实的示例配置文档,特此求助。


核心字段说明

1. 远程写入相关字段(Prometheus/Loki)

这些字段用来把采集到的指标/日志发送到你的Grafana Stack(Prometheus或Loki),需要对应你的服务实际配置:

  • <your_prom_user>/<your_prom_pass>:Prometheus实例开启Basic Auth后的用户名和密码,没开认证可以直接删掉basic_auth块
  • <your_prom_url>:Prometheus的远程写入接口地址,比如http://192.168.1.100:9090/api/v1/write
  • <your_loki_user>/<your_loki_pass>:Loki实例开启Basic Auth后的用户名和密码,同样,没开认证可删除该块
  • <your_loki_url>:Loki的日志推送接口地址,比如http://192.168.1.101:3100/loki/api/v1/push

2. Nginx日志采集相关字段

  • <http_hostname>:你的Nginx服务对应的域名或主机名,比如blog.example.com,用来给日志打标签,方便后续在Grafana中按主机筛选
  • <path to json nginx access log>:Nginx JSON格式访问日志的绝对路径,比如/var/log/nginx/access.log(注意:必须是JSON格式,需要先配置Nginx输出JSON日志)

3. Nginx指标采集补充

原官方示例的metrics.configs.scrape_configs是空的,这是关键问题——要获取Nginx运行指标(连接数、请求数等),必须添加指标采集配置。前提是你的Nginx已经安装ngx_http_stub_status_module模块,并且配置了状态端点(比如/status)。


完整修正后的配置示例

integrations:
  agent:
    enabled: true
    relabel_configs:
    - action: replace
      source_labels:
      - agent_hostname
      target_label: instance
    metric_relabel_configs:
    - action: keep
      regex: (prometheus_target_.*|prometheus_sd_discovered_targets|agent_build.*|agent_wal_samples_appended_total|process_start_time_seconds)
      source_labels:
      - __name__
  prometheus_remote_write:
  - basic_auth:
      password: "your-real-prom-password"
      username: "your-real-prom-username"
    url: "http://192.168.1.100:9090/api/v1/write"
logs:
  configs:
  - clients:
    - basic_auth:
        password: "your-real-loki-password"
        username: "your-real-loki-username"
      url: "http://192.168.1.101:3100/loki/api/v1/push"
    name: integrations
    positions:
      filename: /tmp/positions.yaml
    scrape_configs:
    - job_name: integrations/nginx
      static_configs:
      - targets:
        - localhost
        labels:
          host: "blog.example.com"
          __path__: "/var/log/nginx/access.log"
      # 解析JSON日志的规则,对应你Nginx的JSON日志字段
      json:
        expressions:
          remote_addr: remote_addr
          request_method: request_method
          request_uri: request_uri
          status: status
          body_bytes_sent: body_bytes_sent
metrics:
  configs:
  - name: integrations
    remote_write:
    - basic_auth:
        password: "your-real-prom-password"
        username: "your-real-prom-username"
      url: "http://192.168.1.100:9090/api/v1/write"
    scrape_configs:
    - job_name: nginx-metrics
      static_configs:
      - targets: ["localhost:80"] # Nginx状态端点的地址和端口
      metrics_path: "/status" # 你配置的Nginx状态路径
      relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        regex: "(.*):.*"
        replacement: "$1"
  global:
    scrape_interval: 60s
  wal_directory: /tmp/grafana-agent-wal

关键注意事项

  1. 配置Nginx输出JSON日志:修改Nginx的nginx.conf或站点配置,添加JSON格式的日志模板:
log_format json_log '{"remote_addr":"$remote_addr","request_method":"$request_method","request_uri":"$request_uri","status":$status,"body_bytes_sent":$body_bytes_sent,"request_time":$request_time}';
access_log /var/log/nginx/access.log json_log;
  1. 验证Nginx状态端点:访问http://localhost/status,能看到类似如下内容说明配置正常:
Active connections: 2 
server accepts handled requests
  15 15 15 
Reading: 0 Writing: 1 Waiting: 1 
  1. 重启并检查Agent:配置完成后重启Grafana Agent:systemctl restart grafana-agent,然后查看日志排查错误:journalctl -u grafana-agent -f

内容的提问来源于stack exchange,提问作者user19884620

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:53:12