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

Prometheus无法采集Django /metrics指标问题求助

问题排查与解决

1. 修复NGINX的/metrics代理配置

你的NGINX中location /metrics的proxy_pass格式错误,Unix套接字的正确写法是http://unix:/path/to/socket;,而非你当前的http://<unix_domain>。修改后的配置如下:

server {
    server_name hostname www.hostname ip;

    # 其他原有配置...

    location /metrics {
        proxy_pass http://unix:<location_of_domain_socket>;;
    }

    location / {
        proxy_pass http://unix:<location_of_domain_socket>;;
    }
}

2. 调整Prometheus抓取配置

Prometheus对localhost目标默认使用http://localhost:80请求,但需明确指定抓取路径,同时解决iptables拦截本地80端口请求的问题:

方案A:完善Prometheus Job配置

修改django-app的job配置,明确指定metrics_path和端口:

<default prom config>
[...]
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "django-app"
    scrape_interval: 10s
    metrics_path: "/metrics"
    static_configs:
      - targets: ["localhost:80"]

  - job_name: "node"
    scrape_interval: 10s
    static_configs:
      - targets: ["localhost:9100"]

方案B:放行iptables本地回环80端口请求

你的iptables日志显示127.0.0.1访问80端口被拦截,添加允许规则:

iptables -A INPUT -i lo -p tcp --dport 80 -j ACCEPT
# 保存规则(不同系统命令可能有差异)
iptables-save > /etc/iptables/rules.v4

3. 验证配置有效性

  • 重启NGINX:systemctl restart nginx
  • 重启Prometheus:systemctl restart prometheus
  • 在Prometheus服务器本地执行curl http://localhost/metrics,确认能正常返回指标数据
  • 访问Prometheus的Targets页面(http://localhost:9090/targets),检查django-app目标状态是否正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 14:12:52