• Loki 收集Nginx日志以 grafana 可视化展示


    背景

    通常用ELK来收集Nginx日志的,对于服务器较少的用elk则显得太重了,可以用loki+Promtail+grafana 代替。

    Loki类似elasticsearch,用于存储;Promtail类似fluent,用于收集;grafana类似kibana,用于展示

    先放几张图来展示一下成果:

    这里是用grafana查询日志

    Nginx 配置

    首先修改Nginx日志为json格式:

    1. log_format json_analytics escape=json '{'
    2. '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
    3. '"connection": "$connection", ' # connection serial number
    4. '"connection_requests": "$connection_requests", ' # number of requests made in connection
    5. '"pid": "$pid", ' # process pid
    6. '"request_id": "$request_id", ' # the unique request id
    7. '"request_length": "$request_length", ' # request length (including headers and body)
    8. '"remote_addr": "$remote_addr", ' # client IP
    9. '"remote_user": "$remote_user", ' # client HTTP username
    10. '"remote_port": "$remote_port", ' # client port
    11. '"time_local": "$time_local", '
    12. '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
    13. '"request": "$request", ' # full path no arguments if the request
    14. '"request_uri": "$request_uri", ' # full path and arguments if the request
    15. '"args": "$args", ' # args
    16. '"status": "$status", ' # response status code
    17. '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
    18. '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
    19. '"http_referer": "$http_referer", ' # HTTP referer
    20. '"http_user_agent": "$http_user_agent", ' # user agent
    21. '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
    22. '"http_host": "$http_host", ' # the request Host: header
    23. '"server_name": "$server_name", ' # the name of the vhost serving the request
    24. '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
    25. '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
    26. '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
    27. '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
    28. '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
    29. '"upstream_response_length": "$upstream_response_length", ' # upstream response length
    30. '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
    31. '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
    32. '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
    33. '"scheme": "$scheme", ' # http or https
    34. '"request_method": "$request_method", ' # request method
    35. '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
    36. '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
    37. '"gzip_ratio": "$gzip_ratio", '
    38. '"http_cf_ray": "$http_cf_ray",'
    39. '"geoip_country_code": "$geoip_country_code"'
    40. '}';
    41. access_log /usr/local/nginx/logs/json_access.log json_analytics;

    注意这里的geoip_country_code,用于展示哪个国家的哪个城市访问。需要在Nginx添加geoip的支持。

    首先通过yum安装geoip及lib库

    yum -y install GeoIP GeoIP-data GeoIP-devel
    

    重新编译Nginx,加入--with-http_geoip_module进行添加模块

    yum安装的geoip内置的数据较老,需要下载最新的数据

    1. wget https://dl.miyuru.lk/geoip/dbip/country/dbip.dat.gz
    2. gunzip dbip.dat.gz && mv dbip.dat country.dat
    3. wget https://dl.miyuru.lk/geoip/dbip/city/dbip.dat.gz
    4. gunzip dbip.dat.gz && mv dbip.dat city.dat

    nginx.conf增加下面两行

    1. geoip_city /usr/local/nginx/html/ip/city.dat;
    2. geoip_country /usr/local/nginx/html/ip/country.dat;

    后执行 /usr/local/nginx/sbin/nginx -s reload;现在Nginx的日志是这样的:

    {"msec": "1654072604.175", "connection": "92799", "connection_requests": "1", "pid": "4251", "request_id": "47e117ff2287fb32fb8382aff742334b", "request_length": "1105", "remote_addr": "11.125.67.88", "remote_user": "", "remote_port": "18154", "time_local": "01/Jun/2022:16:36:44 +0800", "time_iso8601": "2022-06-01T16:36:44+08:00", "request": "POST /xx/xx.html HTTP/1.1", "request_uri": "/xx/xx.html", "args": "", "status": "200", "body_bytes_sent": "146", "bytes_sent": "308", "http_referer": "http://172.16.124.225:8080/", "http_user_agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1", "http_x_forwarded_for": "172.16.124.225, 218.104.146.57", "http_host": "xx.com", "server_name": "xx.com", "request_time": "0.704", "upstream": "192.168.96.13:80", "upstream_connect_time": "0.000", "upstream_header_time": "0.700", "upstream_response_time": "0.700", "upstream_response_length": "135", "upstream_cache_status": "", "ssl_protocol": "", "ssl_cipher": "", "scheme": "http", "request_method": "POST", "server_protocol": "HTTP/1.1", "pipe": ".", "gzip_ratio": "", "http_cf_ray": "","geoip_country_code": "US"}
    

    Loki部署

    下载软件

    1. wget https://github.com/grafana/loki/releases/download/v2.5.0/loki-linux-amd64.zip
    2. unzip loki-linux-amd64.zip
    3. mv loki-linux-amd64 loki

    loki的配置文件

    1. auth_enabled: false
    2. server:
    3. http_listen_port: 3100
    4. grpc_listen_port: 9096
    5. common:
    6. path_prefix: /data/loki
    7. storage:
    8. filesystem:
    9. chunks_directory: /data/loki/chunks
    10. rules_directory: /data/loki/rules
    11. replication_factor: 1
    12. ring:
    13. instance_addr: 127.0.0.1
    14. kvstore:
    15. store: inmemory
    16. schema_config:
    17. configs:
    18. - from: 2020-10-24
    19. store: boltdb-shipper
    20. object_store: filesystem
    21. schema: v11
    22. index:
    23. prefix: index_
    24. period: 24h
    25. ruler:
    26. alertmanager_url: http://localhost:9093

    启动loki

    nohup ./loki -config.file=loki-local-config.yaml &
    

    Promtail 部署

    下载软件

    1. wget https://github.com/grafana/loki/releases/download/v2.5.0/promtail-linux-amd64.zip
    2. unzip promtail-linux-amd64.zip
    3. mv promtail-linux-amd64 promtail

    配置文件

    1. server:
    2. http_listen_port: 9080
    3. grpc_listen_port: 0
    4. positions:
    5. filename: /tmp/positions.yaml
    6. clients:
    7. - url: http://192.168.96.239:3100/loki/api/v1/push
    8. scrape_configs:
    9. - job_name: nginx
    10. pipeline_stages:
    11. - replace:
    12. expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})'
    13. replace: '***'
    14. static_configs:
    15. - targets:
    16. - localhost
    17. labels:
    18. job: nginx_access_log
    19. host: appfelstrudel
    20. agent: promtail
    21. __path__: /usr/local/nginx/logs/json_access.log

    启动

    nohup ./promtail --config.file=promtail-local-config.yaml &
    

    grafana展示

    下载安装

    1. wget https://mirrors.cloud.tencent.com/grafana/yum/rpm/grafana-8.5.3-1.x86_64.rpm
    2. yum install -y grafana-8.5.3-1.x86_64.rpm
    3. systemctl start grafana-server

    grafana添加数据源,选Loki。

    输入url:http://localhost:3100 即可

    然后导入 13865 模板即可

    下面这篇文章没实践过,不过里面地图可以对应到城市,本篇的地图只能对应到国家:

    搭建日志聚合grafana&loki 收集Nginx日志_Zz_糖小七的博客-CSDN博客_loki收集nginx

    nginx 设置变量获取国家、城市:

    科技常识:Nginx服务器中配置GeoIP模块来拦截指定国家IP_绿色消费网

    grafana 面板报 "too many outstanding requests" 错误解决方案

    Grafana dashboard shows "too many outstanding requests" after upgrade to v2.4.2 - bytemeta

    grafana 面板报 "maximum of series (500) reached for a single query" 错误解决方案

    maximum of series (500) reached for a single query - Installation - Grafana Labs Community Forums

    grafana 面板报 " net/http: timeout awaiting response headers (Client.Timeout exceeded while awaiting headers)" 错误解决方案

    Configure Grafana | Grafana documentation

    loki 查询超时解决方案 

    Loki datasource timeout at 30s · Issue #42801 · grafana/grafana · GitHub

    loki 用缓存加速查询

    巧用缓存加速Loki查询 - 腾讯云开发者社区-腾讯云

    loki 配置文件范例参考 

    Loki datasource timeout at 30s · Issue #42801 · grafana/grafana · GitHub

  • 相关阅读:
    第20章 接口手册【C++】
    【WebRTC API】媒体设备使用入门
    1004. 最大连续1的个数III(滑动窗口)
    Prometheus-2:blackbox_exporter黑盒监控
    阿里Java面试题剖析:如何保证缓存与数据库的双写一致性?
    HTB靶场之Sandworm
    查看linux开发板的CPU频率
    四、一起学习Java 对象和类
    数据库的约束
    矩阵白化原理及推导
  • 原文地址:https://blog.csdn.net/JineD/article/details/126670107