【问题标题】:How to get nginx logs to display in prometheus?如何让 nginx 日志显示在 prometheus 中?
【发布时间】:2018-08-30 20:30:48
【问题描述】:

我有一个 nginx docker 镜像在随机端口 http://localhost:32774 上运行 我还有一个在http://localhost:9090/ 上运行的 prometheus docker 映像 我想在我的 prometheus 上看到我的 nginx 日志显示

我已经在我的 nginx 容器上设置了 nginx_status,当我 curl 时 当我在容器中时,我可以同时看到 Nginx 页面和 http://localhost/nginx_status 页面

我可以查看http://localhost:9090/graph --Prometheus 我可以在本地浏览器上查看http://localhost:32774 --- nginx 我无法查看http://localhost:32774/nginx_status -- 403 禁止访问 nginxexporter 背后的想法是什么?我将它作为在 localhost:9113 上运行的容器

我的目标是让 nginx 日志显示在 Prometheus 上

这是我的 default.conf

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}
location /nginx_status {
    stub_status on;

    access_log off;
    allow 127.0.0.1;
    allow ::1;
    deny all;
}


#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

【问题讨论】:

  • 我通过删除deny all解决了403禁止
  • 我确实想通过以下正常运行时间 http 请求和结果来监控我的 nginx 网络服务器,有多少失败的请求,我希望 Prometheus 这样做

标签: docker nginx prometheus


【解决方案1】:

首先,prometheus 用于度量,而不是记录。对于日志记录,您可以将 ElasticSearch 与 Logstash 和/或 Filebeat 结合使用。

nginx_exporter 从 nginx 的 status api 中读取数据。因此,nginx 导出器必须有权访问此 api 处于活动状态的 nginx 端口。在大多数“操作指南”情况下,这是端口 8080,但它是可配置的。

https://github.com/nginxinc/nginx-prometheus-exporter 看看这些例子,-nginx.scrape-uri 是 nginx 的 api 的 url / 路径和端口。

https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/#configuring-the-api 看看这里设置你的 api,你必须为此添加或更改一些 nginx 配置。

你也可以像这样创建一个服务器块来启用 nginx api。:

server {
    listen <fill_in_the_ip_of_your_server>:8080;
    location /api {
        api;
        allow all; 

    ###
    # change the 'allow all' if the server block doesn't have any access limitations and is accessible to
    # the world. You won't give the world access to your nginx data.
    # allow takes multiple types of data, the most popular and built-in one is a simple IP in CIDR notation (IP + subnet in bits (192.168.1.1/16 for example will give all addresses in 192.168.x.x. access to this api. /24 will do 192.168.1.x and /32 will fix that specified address only to access that specific server or 'location' block.
    ###

    }
}

之后,您必须在 prometheus 中添加“scrape”端点。 Prometheus 有时将其称为“目标”。请记住,nginx-exporter 的 docker 映像必须能够访问 nginx(-plus) 实例 api,并且 prometheus 机器应该能够访问位于 :9113/metrics 的 nginx-exporter 指标页面。您可以更改 nginx 导出器的端口,但如果该端口尚未在导出器所在的 IP 上使用,则不需要。

还请记住,如果您在 docker 中运行的端口之外的另一个端口上启用您的 api,您应该先终止容器并使用 `-p- 添加端口映射,否则此端口仅在 docker 容器内处于活动状态但从未暴露给它的主机......在这种情况下可能是你的服务器或计算机。

祝你好运!

【讨论】:

  • 通过 nginx-Prometheus-exporter 阅读后,我在文档中注意到从 nginx 的链接逐字复制它是 /stub_status/page 而对于 nginx 加上它是 /api 不应该是 /stub_status_page 相同去执行命令..要导出 NGINX 指标,运行: $ docker run -p 9113:9113 nginx/nginx-prometheus-exporter:0.1.0 -nginx.scrape-uri http://:8080 /api 应该是 /stub_status_page ?
【解决方案2】:

我们开始使用这个导出器 https://github.com/martin-helmich/prometheus-nginxlog-exporter 持续读取 NGINX 日志文件(或任何类型的类似日志文件)并将指标导出到 Prometheus 的帮助工具。

【讨论】:

    【解决方案3】:

    奇怪一,prometheus是用来监控不记录的,而nginx导出器不导出日志,只导出请求数、请求时间、返回码等监控指标。

    奇怪的是,您可以使用 localhost 访问 nginx 而无需考虑自定义端口……但这可以解释错误。

    【讨论】:

      【解决方案4】:

      Prometheus 是一个收集和处理指标的系统,而不是事件记录系统。

      https://prometheus.io/docs/introduction/faq/#how-to-feed-logs-into-prometheus?

      您可以设置fluentd -&gt; ES -&gt; Grafana 来可视化日志。

      nginx_exporter 只是检索 nginx 统计数据和导出。

      【讨论】:

        猜你喜欢
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-16
        • 2012-02-12
        • 1970-01-01
        • 2021-11-18
        相关资源
        最近更新 更多