51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1872|回复: 0
打印 上一主题 下一主题

[原创] linux下对nginx的监控(页面展示、命令查看)

[复制链接]
  • TA的每日心情
    奋斗
    2021-8-6 16:14
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2019-4-1 14:47:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    nginx的状态监控,主要是指nginx的进程数,创建的tcp连接数,当前等待的连接数等。对nginx的状态监控,有助于排查nginx运行中遇到的问题,快速定位。

            监控主要有两种方式,页面展示和通过一些命令来进行。

    页面配置

            此方法需要nginx安装http_stub_status_module模块,这个模块是nginx自带的。但是默认没有安装,需要用户指定安装。可以通过命令查看是否有安装。
    1. [root@izbp14wmlq6ajvhexzq3q0z sbin]# /usr/local/nginx/sbin/nginx -V
    2. nginx version: nginx/1.12.2
    3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
    4. configure arguments: --with-http_stub_status_module
    复制代码

    通过configure arguments可以看到上面是有安装

    如果未安装,在安装的时候,只需要指定

    1. ./configure --with-http_stub_status_module
    复制代码

    即可

    然后修改配置文件nginx.conf,在server块中增加

    1. location /nginx_status {
    2.         # Turn on nginx stats
    3.         stub_status on;
    4.         # I do not need logs for stats
    5.         access_log   off;
    6.         # Security: Only allow access from 192.168.1.100 IP #
    7.         allow 115.192.86.239;
    8.         # Send rest of the world to /dev/null #
    9.         #deny all;
    10. }
    复制代码

    然后重启执行

    1. [root@izbp14wmlq6ajvhexzq3q0z conf]# /usr/local/nginx/sbin/nginx -s relaod
    复制代码

    即可

    页面访问http://127.0.0.1/nginx_status

    1. Active connections: 44
    2. server accepts handled requests
    3. 4974 4974 291733
    4. Reading: 0 Writing: 1 Waiting: 43
    复制代码

    ctive connections:与后端建立的服务连接数

    server accepts handled requests:Nginx总共处理了4974个连接,成功创建了4974次握手,总共处理了291733个请求

    Reading:nginx读取到客户端的Header信息数

    Writing:nginx返回到客户端的Header信息数

    Waiting:开启Keep-alive的情况下,这个值等于 Active -(Reading + Writing)。表示nginx已经处理完成,正在等候下次一次请求的连接数。

    说明:如果Reading + Writing数量比较高,表示当前并发很大;如果Waiting较大,表示处理的很快,已经在等待之后的请求了。


    命令查看


    1 查看哪些ip正在连接

    1. [root@izbp14wmlq6ajvhexzq3q0z ~]# netstat -an
    2. Active Internet connections (servers and established)
    3. Proto Recv-Q Send-Q Local Address           Foreign Address         State      
    4. tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN     
    5. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
    6. tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN     
    7. tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN     
    8. tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
    9. tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN     
    10. tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN     
    11. tcp        0      0 172.16.135.185:8082     118.31.52.247:33466     TIME_WAIT  
    12. tcp        0      0 172.16.135.185:49354    111.230.187.248:62523   ESTABLISHED
    13. tcp        0      0 172.16.135.185:8082     118.31.52.247:33388     TIME_WAIT  
    14. tcp        0      0 172.16.135.185:40114    100.100.45.158:80       ESTABLISHED
    复制代码

    2 查看当前nginx运行状态

    1. [root@izbp14wmlq6ajvhexzq3q0z conf]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    2. CLOSE_WAIT 2
    3. ESTABLISHED 52
    4. TIME_WAIT 53
    复制代码

    这是在我自己的服务器上运行的,显示的状态只有:CLOSE_WAIT、ESTABLISHED、TIME_WAIT。实际上这里的状态会有很多种。

    CLOSE_WAIT:表示被动关闭

    ESTABLISHED:当前并发连接数

    TIME_WAIT:主动关闭,处理完毕,等待超时结束的连接数


    3 查看nginx运行进程数

    1. [root@izbp14wmlq6ajvhexzq3q0z ~]# ps -ef | grep nginx | wc -l
    2. 3
    复制代码

    4 查看web服务器进程数

    1. [root@izbp14wmlq6ajvhexzq3q0z ~]# ps -ef | grep httpd | wc -l
    2. 1
    复制代码

    5 查看httpd占用内存的平均数

    1. [root@izbp14wmlq6ajvhexzq3q0z ~]# ps aux|grep -v grep|awk '/httpd/{sum+=$6;n++};END{print sum/n}'
    2. 956
    复制代码

    6 查看nginx中访问前10的ip

    1. [root@izbp14wmlq6ajvhexzq3q0z ~]# awk '{print $1}' /usr/local/nginx/logs/access.log | sort
    2. | uniq -c | sort -nr -k1 | head -n 10
    3.   35343 115.192.86.239
    4.      59 47.94.157.167
    5.       3 60.191.52.254
    6.       2 85.211.202.152
    7.       2 80.82.70.187
    8.       2 156.222.245.199
    9.       2 156.203.252.30
    10.       2 103.41.127.142
    11.       1 94.243.246.80
    12.       1 89.40.84.154
    复制代码

    格式:awk '{print $1}' 日志文件路径 | sort | uniq -c | sort -nr -k1 | head -n 10


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-20 05:08 , Processed in 0.063298 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表