首页 > 系统相关 >一套简单的 Nginx 日志展示

一套简单的 Nginx 日志展示

时间:2023-04-07 18:02:41浏览次数:36  
标签:http 展示 time request Nginx upstream 日志

采用架构 Nginx+Promtail+Loki+Grafana 做个简单的 Nginx 日志展示

本文无安装,简要点出配置需求。Nginx 用编译安装(本文不做叙述),Promtail 和 Loki 都选用二进制的方式进行安装,直接下载对应版本的二进制文件,解压后指定配置文件启动即可。

promtail 配置文件内容:

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: nginx
  pipeline_stages:
  - replace:
      expression: '(?:[0-9]{1,3}\.){3}([0-9]{1,3})'
      replace: '***'
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx_access_log
      host: expatsxxxxs
      agent: promtail
      __path__: /var/log/nginx/expatshxxxxs.access.log

日志收集工作完成后,在 Nginx 中需要修改日志格式,修改 Nginx 的日志格式为 Json 格式,配置如下:

log_format json_analytics escape=json '{'
                            '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                            '"connection": "$connection", ' # connection serial number
                            '"connection_requests": "$connection_requests", ' # number of requests made in connection
                    '"pid": "$pid", ' # process pid
                    '"request_id": "$request_id", ' # the unique request id
                    '"request_length": "$request_length", ' # request length (including headers and body)
                    '"remote_addr": "$remote_addr", ' # client IP
                    '"remote_user": "$remote_user", ' # client HTTP username
                    '"remote_port": "$remote_port", ' # client port
                    '"time_local": "$time_local", '
                    '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                    '"request": "$request", ' # full path no arguments if the request
                    '"request_uri": "$request_uri", ' # full path and arguments if the request
                    '"args": "$args", ' # args
                    '"status": "$status", ' # response status code
                    '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                    '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                    '"http_referer": "$http_referer", ' # HTTP referer
                    '"http_user_agent": "$http_user_agent", ' # user agent
                    '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                    '"http_host": "$http_host", ' # the request Host: header
                    '"server_name": "$server_name", ' # the name of the vhost serving the request
                    '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                    '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                    '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                    '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                    '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                    '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                    '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                    '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                    '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                    '"scheme": "$scheme", ' # http or https
                    '"request_method": "$request_method", ' # request method
                    '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                    '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray",'
                    '"geoip_country_code": "$geoip_country_code"'
                    '}';

由于需要在 nginx 中添加 geoip 模块,所以我们要安装 geoip

yum -y install GeoIP GeoIP-data GeoIP-devel

重新编译 Nginx,通过 --with-http_geoip_module 添加 nginx 的 geoip 模块

编译完成后,在 objs 目录下生成新的 Nginx 可执行文件,替换原先的,通过 kill -USR2 信号,升级 Nginx。

此时配置的 log_format 不会再报错 geoip 指令找不到,配置完成后,配置网站的 access 日志,引用刚才配置的 json 格式的 log_format。

此时的日志格式已经转为 JSON 格式,接着安装 grafana,最快捷的方式,通过 docker 直接起一个

docker run -d -p 3000:3000 grafana/grafana

启动后,通过 admin/admin 默认用户名密码登录,登陆后提示重置密码,之后进入 grafana 界面直接添加数据源 loki

一套简单的 Nginx 日志展示_Nginx

先通过 explore 查询,日志是否存入 loki

一套简单的 Nginx 日志展示_日志_02

可以看到,日志已经存入 loki ,接着添加 Dashboard,通过 ID 导入

一套简单的 Nginx 日志展示_日志_03

一套简单的 Nginx 日志展示_Nginx_04

一套简单的 Nginx 日志展示_Nginx_05

可以看到地图没展示,是因为缺少grafana-worldmap-panel插件,可以通过命令行安装

grafana-cli plugins install grafana-worldmap-panel

装好后重启Grafana,重新加载dashboard

一套简单的 Nginx 日志展示_日志_06

这样一套简单的日志展示就搭建好了,适合小体量服务器。

标签:http,展示,time,request,Nginx,upstream,日志
From: https://blog.51cto.com/u_15932009/6176461

相关文章

  • 日志和文件共享
    rsyslog管理系统日志术语facility:设施,从功能或程序上对日志进行归类#内置分类auth,authpriv,cron,daemon,ftp,kern,lpr,mail,news,security(auth),user,uucp,syslog#自定义的分类local0-local7Priority优先级别,从低到高排序debug,info,notice,warn(w......
  • django记录基础操作日志
    1.新增middleware.py中间件:需要在setting.py注册classLoggingMiddleware:"""日志记录模块:操作用户、操作ip、请求路径、请求方式、请求时间"""def__init__(self,get_response):self.get_response=get_responsedef__call__(self,......
  • Linux安装nginx完整步骤
    一、安装依赖(安装过的跳过)yum-yinstallgccgcc-c++automakepcrepcre-develzlibzlib-developensslopenssl-develgd-devel二、下载nginx稳定版wgethttp://nginx.org/download/nginx-1.16.1.tar.gz三、解压–编译–安装解压tar-zxvfnginx-1.16.1.tar.gz进......
  • Loki 日志管理
    一、lokiInstallation|GrafanaLokidocumentation1、loki介绍Loki是GrafanaLabs团队发布的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统。项目受Prometheus启发,官方的介绍就是:LikePrometheus,butforlogs.,类似于Prometheus的日志系统 二、docker......
  • C#添加log4日志
    第一步导入log4net在vs的程序包管理器控制台中执行命令NuGet\Install-Packagelog4net-Version2.0.0第二步加帮助类HttpHelperusingSystem;usingSystem.Reflection;usinglog4net;usinglog4net.Config;namespacePuYangHiqs{publicclassLogger{......
  • ctfshow-web4(Nginx日志写shell)
    这是一道有关Nginx日志文件写入shell的题目。  一、基础知识/etc/nginx/nginx.conf:该文件是Ngnix的配置的文件,该文件保存着能够影响服务器运行的配置信息。有时会泄露敏感信息。该文件支持php解析。/var/log/nginx/access.log:该文件是Ngnix的访问日志文件,Ngnix会把每个......
  • Nginx 安装和简单配置
    1.Nginx下载(以1.21.6为例)下载链接2.解压使用ftp工具上传到/opt路径下,用下面的命令解压tar-zxvfnginx-1.21.6.tar.gz3.编译和安装解压后的/opt/nginx-1.21.6/目录下有一个config的文件,执行该文件./config#如果需要开启ssl模块,需要执行以下命令(ssl模块开......
  • Mybatis学习 _04 _日志学习
    Mybatis学习_04_日志学习1、日志日志:一般用来查找代码出错的适合使用,使得程序员更清楚快捷的查找问题!!!1、方法一:<settings> <settingname="logImpl"value="STDOUT_LOGGING"/></settings>2、方法二:Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息......
  • 47 openEuler搭建Nginx服务器-配置文件说明和管理模块
    47openEuler搭建Nginx服务器-配置文件说明和管理模块47.1Nginx配置文件说明当nginx服启动后,默认情况下它会读取如表2所示的配置文件。表2配置文件说明文件说明/etc/nginx/nginx.conf主要的配置文件/etc/nginx/conf.d配置文件的辅助目录,这些配置文件也被包含......
  • rsyslog实现日志的集中管理
    配置远程日志服务器->实现日志的集中管理环境:两台服务器server端client端1)server端配置[root@centos7-xinsz08~]#vim/etc/rsyslog.conf#ProvidesTCPsyslogreception$ModLoadimtcp$InputTCPServerRun514重启[root@centos7-xinsz08~]#systemctlrestartrsyslog3......