docker - 如何让 nginx 日志显示在 prometheus 中?
问题描述
我有一个在随机端口http://localhost:32774上运行的 nginx docker 映像我还有一个在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;
#}
}
解决方案
First things first, prometheus is for metrics, not logging. For logging, you could use ElasticSearch in combination with Logstash and/or Filebeat.
The nginx_exporter reads the data from the status api of nginx. So, the nginx exporter must have access to the port of nginx where this api is active. In most 'how-to' cases, this is port 8080, it is however configurable.
https://github.com/nginxinc/nginx-prometheus-exporter
Take a look at these examples, where the -nginx.scrape-uri
is the url / path and port to the api of nginx.
https://docs.nginx.com/nginx/admin-guide/monitoring/live-activity-monitoring/#configuring-the-api Take a look here to setup your api, you have to add or alter some nginx config for this.
You could also just create a server block like this to enable the 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.
###
}
}
After that, you have to add the 'scrape' endpoint in prometheus. Prometheus calls this sometimes 'targets'. Keep in mind that the docker image of the nginx-exporter must have access to the nginx(-plus) instances api and that the prometheus machine should be able to access the nginx-exporter metrics page at :9113/metrics. You could change the port of the nginx exporter, but it is not required if this port is not already in use at the IP where the exporter lives.
Keep also in mind that if you enable your api on another port than the port running in docker, you should kill the container and add a port mapping with `-p- first, otherwise this port is only active inside the docker container but never exposed to its host... in this case probably your server or computer.
Good luck!
推荐阅读
- google-kubernetes-engine - 可以将 Ingress 附加到 GKE 中的负载均衡器类型服务吗?
- javascript - Ubuntu 为 mysql_secure_installation 设置密码
- macos - OSX Catalina 上的 Delphi 10.4 Firedac 错误 314
- vba - VBA 用户窗体中的 VLookup - 字符串查找值
- flutter - Flutter 创建带有文本和加载器的启动画面
- php - 使用边界半径的圆角动态生成 li
- c# - 找不到类型或命名空间名称 Unity
- ssl - 使用 github 页面的安全 https 重定向和 letencrypt 不起作用
- python - 循环遍历多列以在数字列中查找字符串?
- key - 公钥不存在或不可读