Redian新闻
>
Linux 这套 Nginx 日志解决方案,真香!

Linux 这套 Nginx 日志解决方案,真香!

公众号新闻

来自:入门小站

最近客户有个新需求,就是想查看网站的访问情况,由于网站没有做 google 的统计和百度的统计,所以访问情况,只能通过日志查看,通过脚本的形式给客户导出也不太实际,给客户写个简单的页面,咱也做不到
成熟的日志解决方案,那就是 ELK,还有现在比较火的 Loki,(当然还有很多其他解决方案,比如 Splunk、Datadog 等),那我们这个小网站,小体量,必然是选择 Loki 来做
所以这次就采用 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"'
                    '}';
配置文件中,需要注意的是 geoip 的配置,需要在 nginx 中添加 geoip 模块
首先通过 yum 安装 geoip 及 lib 库
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
查看此时的日志格式
{"msec""1633430998.322""connection""4""connection_requests""1""pid""29887""request_id""40770fec38c2e5a68714df5f7a67283d""request_length""392""remote_addr""106.19.96.55""remote_user""""remote_port""43746""time_local""05/Oct/2021:18:49:58 +0800""time_iso8601""2021-10-05T18:49:58+08:00""request""GET / HTTP/2.0""request_uri""/""args""""status""200""body_bytes_sent""60949""bytes_sent""61222""http_referer""https://cn.bing.com/search?q=expat+tourism+agent+in+china&go=Search&qs=n&form=QBRE&sp=-1&pq=expat+tourism+agent+in+chi&sc=0-26&sk=&cvid=8BD2D4B79B3A4FA682571CB5BC7334D4""http_user_agent""Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1""http_x_forwarded_for""""http_host""www.expatsholidays.com""server_name""www.expatsholidays.com""request_time""0.003""upstream""127.0.0.1:9000""upstream_connect_time""0.000""upstream_header_time""0.002""upstream_response_time""0.002""upstream_response_length""60991""upstream_cache_status""""ssl_protocol""TLSv1.2""ssl_cipher""ECDHE-RSA-AES128-GCM-SHA256""scheme""https""request_method""GET""server_protocol""HTTP/2.0""pipe"".""gzip_ratio""""http_cf_ray""","geoip_country_code"""}
{"msec""1633430998.709""connection""4""connection_requests""2""pid""29887""request_id""430fd53a457ea580c47e9b055da2b4d0""request_length""56""remote_addr""106.19.96.55""remote_user""""remote_port""43746""time_local""05/Oct/2021:18:49:58 +0800""time_iso8601""2021-10-05T18:49:58+08:00""request""GET /maps/api/js?ver=5.6.1 HTTP/2.0""request_uri""/maps/api/js?ver=5.6.1""args""ver=5.6.1""status""200""body_bytes_sent""41262""bytes_sent""41406""http_referer""https://www.expatsholidays.com/""http_user_agent""Mozilla/5.0 (iPhone; CPU iPhone OS 13_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Mobile/15E148 Safari/604.1""http_x_forwarded_for""""http_host""www.expatsholidays.com""server_name""www.expatsholidays.com""request_time""0.000""upstream""""upstream_connect_time""""upstream_header_time""""upstream_response_time""""upstream_response_length""""upstream_cache_status""""ssl_protocol""TLSv1.2""ssl_cipher""ECDHE-RSA-AES128-GCM-SHA256""scheme""https""request_method""GET""server_protocol""HTTP/2.0""pipe"".""gzip_ratio""3.06""http_cf_ray""","geoip_country_code"""}
已经转为 JSON 格式,接着安装 grafana,最快捷的方式,通过 docker 直接起一个
docker run -d -p 3000:3000 grafana/grafana
启动后,通过 admin/admin 默认用户名密码登录,登陆后提示重置密码,之后进入 grafana 界面
我们直接添加数据源 loki
先通过 explore 查询,日志是否存入 loki
可以看到,日志已经存入 loki
接着添加 Dashboard,通过 ID 导入
导入如下
可以看到,这里地图没展示,缺少grafana-worldmap-panel插件,我们装一下,通过命令行
grafana-cli plugins install grafana-worldmap-panel
安装完成后,重启 grafana,重新查看 dashboard
地图可以展示,这里因为地图的地址是国外的地址,所以地图的图片无法加载,可以通过反代的方式解决
ok,今天内容就这些,通过简单快速的方式,为客户呈现网站的详细访问情况,Loki+Grafana 是绝佳的组合

END

官方站点:www.linuxprobe.com

Linux命令大全:www.linuxcool.com

刘遄老师QQ:5604215

Linux技术交流群:3861509

(新群,火热加群中……)

想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
草书《兰亭序》Linux 上 Nginx 获得最佳性能的 8 种方法【周末得吃好的】卤牛腱+芝麻烧饼+酸辣汤+杏仁蛋糕Cloudflare用Rust重写Nginx C模块,构建没有Nginx的未来我,哥大毕业去新加坡做Banker,一天八小时拿98w年薪,真香!真香!一个轻量级的日志追踪框架,10 分钟即可接入!上海睿康生物:以IVD项目为导向的完整解决方案,真正打通质谱临床落地路径九种分布式ID解决方案,总有一款适合你!“划划手机,不上班也赚钱”:最适合宝妈的神仙副业,真香!Redis,真香!我,IC毕业生去新加坡,2年拿绿卡,真香!聚焦提供优化能源生产、使用效率的工业认知AI解决方案,「邦定智慧」获千万元Pre-A轮融资|早起看早期薪酬倒挂影响员工士气?这有一份解决方案,请查收世界上只有两个 Linux 发行版:Arch Linux 与其它 | Linux 中国SpringBoot 多数据源及事务解决方案【Lakeridge雷克萨斯】5月 RX NX 巨大优惠 NX350H 少量现货, 找Allen金牌销售 另高价现金收车刚推出半个月,这套运营解决方案就征服了半个游戏圈【雨水】心雨细无声· 写在老爸“七七”之日华为制造军团发布软件研发工具链解决方案,助力企业研发提质增效“中央公园五人”冤狱事件,那是川普对美国的第一次尖叫如何在 Rocky Linux 9 / AlmaLinux 9 上安装 KVM | Linux 中国还在手动配置Nginx?太LOW了,这个超强大的 Nginx 可视化管理工具太牛逼了!ChatGPT代写简历,真香!性格与命运— 言慧珠宁折不弯SpringBoot 实现 MySQL 百万级数据量导出并避免 OOM 的解决方案研发本体感知AI解决方案,「咸兑科技」完成1亿元B轮融资丨36氪首发广和通携手联发科发布5G FWA解决方案,助力全球5G市场“更上一层楼”【Lakeridge雷克萨斯】6月 RX NX 巨大优惠 NXH RXH有现货, NX利息低至1.9 - 另高价现金收车MyBatis-Plus 还手写 Join 联表查询?一个依赖轻松搞定,真香!升级私域运营解决方案,有赞坚定做商家成长陪伴者超强大的 Nginx 可视化管理平台 Nginx-Proxy-Manager 中文入门指南增程式,真香!Linux 服务器大量的 log 日志,如何正确看日志快速定位错误?TUXEDO Stellaris 16(Gen5)是目前所能找到的终极 Linux 笔记本电脑 | Linux 中国从单点工具到一体化解决方案,SaaS公司如何实现产品跃迁?|甲子光年
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。