如何获取Nginx日志以普罗米修斯显示?

时间:2018-08-30 20:30:49

标签: docker nginx prometheus

我有一个在随机端口https://facebook.github.io/react-native/docs/0.56/webview#scrollenabled上运行的nginx docker映像 我还有一个在http://localhost:32774上运行的prometheus docker映像 我想在我的普罗米修斯上看到我的Nginx日志

我已经在我的nginx容器上以及当我卷曲时设置了nginx_status 当我在容器中时,我能够同时看到Nginx页面和http://localhost:9090/页面

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

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

这是我的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;
#}
}

4 个答案:

答案 0 :(得分:1)

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

nginx_exporter从nginx的状态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”端点。普罗米修斯称这有时为“目标”。请记住,nginx-exporter的docker映像必须有权访问nginx(-plus)实例api,并且prometheus机器应能够访问位于:9113 / metrics的nginx-exporter指标页面。您可以更改nginx导出器的端口,但是如果导出器所在的IP上尚未使用此端口,则不需要此端口。

还请记住,如果您在docker中运行的端口之外的其他端口上启用api,则应杀死该容器并首先使用`-p-添加一个端口映射,否则该端口仅在docker容器内有效但从未暴露给它的主机...在这种情况下,可能是您的服务器或计算机。

祝你好运!

答案 1 :(得分:0)

奇怪的是,prometheus用于监视而不是日志记录,nginx导出器不导出日志,它仅导出监视指标,例如请求数,请求时间和返回的代码。

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

答案 2 :(得分:0)

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

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

您可以设置fluentd -> ES -> Grafana以可视化日志。

nginx_exporter仅检索nginx统计信息并导出。

答案 3 :(得分:0)

我们在这里使用此导出器 https://github.com/martin-helmich/prometheus-nginxlog-exporter 不断读取NGINX日志文件(或任何类似日志文件)并将度量标准导出到Prometheus的帮助程序工具。

相关问题