nginx實(shí)時(shí)請求監(jiān)測的項(xiàng)目實(shí)踐
一、ngxtop
1、ngxtop介紹
ngxtop是一個(gè)實(shí)時(shí)命令行工具,可以分析nginx訪問日志,以表格形式將http請求的主要指標(biāo)直觀展示。ngxtop將nginx的訪問請求數(shù)據(jù)實(shí)時(shí)動(dòng)態(tài)展示,并支持按字段過濾請求。
2、安裝
centos環(huán)境安裝
# 安裝python yum install epel-release yum install python-pip # 安裝ngxtop pip install ngxtop
ubuntu環(huán)境安裝,測試系統(tǒng)為Ubuntu 24.04.2 LTS
apt upgrade -y apt install python3 sudo apt install pipx pipx ensurepath source ~/.bashrc pipx install ngxtop
3、參數(shù)說明
- -c:指定nginx配置文件,以從中解析log_format和access_log路徑。如ngxtop -c /usr/local/nginx/conf/nginx.conf
- -i:添加過濾條件。如ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'
- -p:添加過濾條件,與-i的區(qū)別是-p在讀取日志前過濾,不匹配的行不會(huì)讀取,-i是在讀取日志后過濾,需要讀取全部日志行。
- -l:指定access-log文件。如ngxtop -l /usr/local/nginx/logs/access_log.log
- -f:指定nginx日志格式,與log_format指令定義的格式一致。
- --no-follow:默認(rèn)情況ngxtop只會(huì)解析最新的訪問日志,此參數(shù)可以強(qiáng)制解析access-log的當(dāng)前內(nèi)容。如ngxtop -c /usr/local/nginx/conf/nginx.conf --no-follow
- -t:在實(shí)時(shí)監(jiān)控模式下,數(shù)據(jù)刷新時(shí)間間隔,默認(rèn)2秒。如ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1
- -g:group by分組字段,默認(rèn)按request_path字段分組。如按客戶端IP分組:ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr
- -w:對(duì)分組結(jié)果的篩選字段,類似sql的having效果。如ngxtop -c /usr/local/nginx/conf/nginx.conf -w 'avg_bytes_sent > 30' -t 1
- -o:指定排序字段。如ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1
- -n:設(shè)置顯示的記錄條數(shù),默認(rèn)10條。如ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5
- -a:在分析結(jié)果中添加聚合字段,如sum、avg、min、max等字段。
- -v:啟用詳細(xì)輸出。
- -d:調(diào)試信息,打印每一行及解析記錄。
- -h:打印幫助信息。
- --version:打印版本信息。
二 、測試
1、指定配置文件
ngxtop -c /usr/local/nginx/conf/nginx.conf

2、加過濾條件
ngxtop -c /usr/local/nginx/conf/nginx.conf -i 'status == 200'

3、解析當(dāng)前access_log
ngxtop -c /usr/local/nginx/conf/nginx.conf --no-follow

4、指定分組條件
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1 -g remote_addr

5、設(shè)置刷新時(shí)間為1秒
ngxtop -c /usr/local/nginx/conf/nginx.conf -t 1
6、排序
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1

7、顯示前5個(gè)結(jié)果
ngxtop -c /usr/local/nginx/conf/nginx.conf -o count -t 1 -n 5

到此這篇關(guān)于nginx實(shí)時(shí)請求監(jiān)測的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)nginx實(shí)時(shí)請求監(jiān)測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx刷新頁面出現(xiàn)404解決方案(親測有效)
本文主要介紹了nginx刷新頁面出現(xiàn)404解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Windows下Nginx的啟動(dòng)停止重啟等命令操作過程
最近在window 上安裝了nginx ,對(duì)nginx 的操作不是很熟,故在此記錄一下常用操作,本文詳細(xì)介紹了Windows下Nginx的啟動(dòng),停止,重啟等命令,需要的朋友可以參考下2024-03-03
Nginx配置多臺(tái)機(jī)器實(shí)現(xiàn)負(fù)載均衡的教程詳解
這篇文章主要為大家詳細(xì)介紹了Nginx配置多臺(tái)機(jī)器實(shí)現(xiàn)負(fù)載均衡的相關(guān)教程,文中的示例代碼簡潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

