• Nginx服务之监控模块vts


    目录

    一、准备工作

    二、系统初始化设置

    三、安装Nginx

    1、安装依赖环境

    2、编译安装

    2.1、解压安装包

    2.2、安装组件

    2.3、make && make install

    3、安装优化

    4、 修改/usr/local/nginx/conf/nginx.conf的配置文件

    四、验证

    五、Nginx服务监控模块版一键安装shell脚本


    一、准备工作

    准备nginx-module-vts-master.zip、nginx-1.15.9.tar.gz源码包放到/opt目录下

    二、系统初始化设置

    1. #!/bin/bash
    2. #################################关闭防火墙#######################################
    3. systemctl stop firewalld.service &> /dev/null
    4. if [ $? -eq 0 ];then
    5. echo "防火墙已关闭"
    6. else
    7. echo "防火墙关闭失败,请重新操作"
    8. break
    9. fi
    10. systemctl disable firewalld.service &> /dev/null
    11. if [ $? -eq 0 ];then
    12. echo "防火墙已设置为开机不启动"
    13. else
    14. echo "防火墙开机不启动设置失败,请重新开始"
    15. break
    16. fi
    17. ###################################关闭安全中心#####################################
    18. setenforce 0 ####
    19. echo "selinux已关闭"
    20. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
    21. grep -w "SELINUX=disabled" /etc/selinux/config &> /dev/null
    22. if [ $? -eq 0 ];then
    23. echo "selinux已设置为永久关闭"
    24. else
    25. echo "selinux设置永久关闭失败"
    26. break
    27. fi
    28. ################################检测调试网卡网络状态##################################
    29. ping -c1 -w1 -i0.2 www.baidu.com &> /dev/null
    30. if [ $? -eq 0 ];then
    31. echo "网络正常"
    32. else
    33. sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/' /etc/sysconfig/network-scripts/ifcfg-ens33
    34. read -p "请输入IP地址: " ip
    35. read -p "请输入子网掩码: " mask
    36. read -p "请输入网关地址: " way
    37. read -p "请输入DNS: " dns
    38. cat<<EOF>>/etc/sysconfig/network-scripts/ifcfg-ens33
    39. IPADDR=$ip
    40. NETMASK=$mask
    41. GATEWAY=$way
    42. DNS=$dns
    43. EOF
    44. systemctl restart network &>/dev/null
    45. ping -c2 www.baidu.com &> /dev/null
    46. if [ $? -eq 0 ];then
    47. echo "网络正常"
    48. else
    49. echo "网络设置失败"
    50. fi
    51. fi
    52. ################################# 修改主机名 ##########################################
    53. read -p "修改主机名:需要输入1;不需要输入2: " xuanze
    54. case $xuanze in
    55. 1)
    56. read -p "输入新的主机名: " xin
    57. hostnamectl set-hostname $xin
    58. su
    59. ;;
    60. 2)
    61. echo "nice"
    62. ;;
    63. *)
    64. echo "输入错误!"
    65. esac

    执行结果

    1. 防火墙已关闭
    2. 防火墙已设置为开机不启动
    3. selinux已关闭
    4. selinux已设置为永久关闭
    5. 网络正常
    6. 修改主机名:需要输入1;不需要输入2: 1
    7. 输入新的主机名: zwb

    三、安装Nginx

    1、安装依赖环境

    yum -y install gcc gcc-c++ pcre-devel zlib-devel make pcre zlib openssl openssl-devel

    2、编译安装

    2.1、解压安装包

    1. cd /opt ##进入压缩包位置
    2. tar -xzvf nginx-1.15.9.tar.gz ##解压nginx-1.15.9压缩包
    3. unzip nginx-module-vts-master.zip ## 解压压缩包
    4. mv nginx-module-vts-master /usr/local/ ##复制nginx-module-vts-master到/usr/local/

    2.2、安装组件

    1. cd /opt/nginx-1.15.9/
    2. ./configure \
    3. --prefix=/usr/local/nginx \
    4. --user=nginx \
    5. --group=nginx \
    6. --add-module=/usr/local/nginx-module-vts-master

    2.3、make && make install

    1. make && make install
    2. ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ ##建立软连接到/usr/local/sbin/方便执行
    3. ##时不需要以绝对路径方式执行
    4. nginx -V ### 查看安装的模块情况
    5. useradd -M -s /sbin/nologin nginx ###添加用户
    6. nginx -t ##检查配置文件语法是否有问题
    7. nginx ##启动

    3、安装优化

    配置nginx.service文件,并赋予权限

    1. [root@zwb nginx-1.15.9]# vim /usr/lib/systemd/system/nginx.service
    2. [Unit]
    3. Description=nginx
    4. After=network.target
    5. [Service]
    6. Type=forking
    7. PIDFile =/usr/local/nginx/logs/nginx.pid
    8. ExecStart=/usr/local/nginx/sbin/nginx
    9. ExecrReload=/bin/kill -s HUP $MAINPID
    10. ExecrStop=/bin/kill -s QUIT $MAINPID
    11. PrivateTmp=true
    12. [Install]
    13. WantedBy=multi-user.target
    14. chmod 754 /lib/systemd/system/nginx.service ##赋予权限
    15. systemctl enable nginx --now ##立即启动并设置为开机启动

    4、 修改/usr/local/nginx/conf/nginx.conf的配置文件

    1. vim /usr/local/nginx/conf/nginx.conf
    2. 1、取消65~71行的注释符#号
    3. 65 location ~ \.php$ {
    4. 66 root html;
    5. 67 fastcgi_pass 127.0.0.1:9000;
    6. 68 fastcgi_index index.php;
    7. 69 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    8. 70 include fastcgi_params;
    9. 2、75 行增加index.php
    10. 45 index index.html index.htm index.php;
    11. 3、第20行增加
    12. vhost_traffic_status_zone;
    13. log_format main '{ "@timestamp": "$time_local", '
    14. '"@fields": { '
    15. '"uri":"$request_uri",'
    16. '"url":"$uri",'
    17. '"upstream_addr":"$upstream_addr",'
    18. '"remote_addr": "$remote_addr", '
    19. '"remote_user": "$remote_user", '
    20. '"body_bytes_sent": "$body_bytes_sent", '
    21. '"host":"$host",'
    22. '"server_addr":"$server_addr",'
    23. '"request_time": "$request_time", '
    24. '"request_time":"$request_time",'
    25. '"status":"$status",'
    26. '"request": "$request", '
    27. '"request_method": "$request_method", '
    28. '"size":$body_bytes_sent,'
    29. '"upstream_time":"$upstream_response_time"'
    30. '"http_referrer": "$http_referer", '
    31. '"body_bytes_sent":"$body_bytes_sent", '
    32. '"http_x_forwarded_for": "$http_x_forwarded_for", '
    33. '"http_user_agent": "$http_user_agent" } }';
    34. 4、在
    35. location / {
    36. root html;
    37. index index.html index.htm;
    38. } 下增加内容
    39. location /status {
    40. vhost_traffic_status_display;
    41. vhost_traffic_status_display_format html;
    42. systemctl restart nginx.service ###更改配置文件后需要重启服务

    四、验证

    登录查看参数(这个IP是我虚拟机的ens33的地址)还请自行查看自己的ip地址。

    Server main

    host:主机名

    version:版本

    version:存活时间

    connections :连接数

    ①active :当前活动的客户端连接数

    ②reading:读取客户端连接的总数

    ③writing:写入客户端连接的总数

    ④waiting:正在等待的客户端连接总数

    requests:请求的客户端连接总数

    ①accepted:接受的客户端连接总数

    ②handled:已处理的客户端连接总数

    ③total:总计客户端连接数

    shared memory:共享内存

    ①name:指定的共享内存名

    ②maxsize:配置中指定的共享内存的最大大小限制

    ③usedsize:共享内存当前大小

    ④usednode:共享内存中当前使用的节点数

    server zones

    requests:从客户端接收的客户端请求总数

    ①total:总数

    ②req/s:单位时间数

    ③time:时间

    responses:状态代码分别为1xx、2xx、3xx、4xx、5xx的响应数

    cache:

    ①miss:未命中的缓存数

    ②bypass:绕过缓存旁路数

    ③expired:过期缓存数

    ④stale:失效缓存数

    ⑤updating:缓存更新次数

    ⑥revalidated:重新验证的缓存数

    ⑦hit:命中缓存数

    ⑧scarce:未达缓存要求的请求总数

    ⑨total:缓存总数
     

    五、Nginx服务监控模块版一键安装shell脚本

    1. #!/bin/bash
    2. rm -f /var/run/yum.pid
    3. ##################系统初始化操作################
    4. systemctl stop firewalld.service &> /dev/null
    5. if [ $? -eq 0 ];then
    6. echo "防火墙关闭成功"
    7. else
    8. echo "防火墙关闭失败"
    9. exit
    10. fi
    11. systemctl disable firewalld.service &> /dev/null
    12. if [ $? -eq 0 ];then
    13. echo "防火墙关闭开机启动成功"
    14. else
    15. echo "防火墙关闭开机启动失败"
    16. exit
    17. fi
    18. setenforce 0
    19. if [ $? -eq 0 ];then
    20. echo "selinux关闭成功"
    21. else
    22. echo "selinux关闭失败"
    23. exit
    24. fi
    25. ##安装依赖包
    26. yum -y install gcc gcc-c++ pcre-devel zlib-devel make pcre zlib openssl openssl-devel
    27. ##编译安装
    28. cd /opt
    29. if [ -f nginx-1.15.9.tar.gz ];then
    30. tar -xzvf nginx-1.15.9.tar.gz
    31. else
    32. echo "nginx-1.15.9.tar.gz不存在"
    33. exit
    34. fi
    35. if [ -f nginx-module-vts-master.zip ];then
    36. unzip nginx-module-vts-master.zip
    37. mv nginx-module-vts-master /usr/local/
    38. else
    39. echo "nginx-module-vts-master不存在"
    40. exit
    41. fi
    42. cd /opt/nginx-1.15.9/
    43. ./configure \
    44. --prefix=/usr/local/nginx \
    45. --user=nginx \
    46. --group=nginx \
    47. --add-module=/usr/local/nginx-module-vts-master \
    48. make && make install
    49. ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
    50. id nginx &> /dev/null
    51. if [ $? -eq 0 ];then
    52. echo "nginx用户已存在"
    53. else
    54. useradd -M -s /sbin/nologin nginx
    55. fi
    56. cat<<EOF>/usr/lib/systemd/system/nginx.service
    57. [Unit]
    58. Description=nginx
    59. After=network.target
    60. [Service]
    61. Type=forking
    62. PIDFile =/usr/local/nginx/logs/nginx.pid
    63. ExecStart=/usr/local/nginx/sbin/nginx
    64. ExecrReload=/bin/kill -s HUP $MAINPID
    65. ExecrStop=/bin/kill -s QUIT $MAINPID
    66. PrivateTmp=true
    67. [Install]
    68. WantedBy=multi-user.target
    69. EOF
    70. chmod 754 /lib/systemd/system/nginx.service
    71. cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
    72. cat<<EOF>/usr/local/nginx/conf/nginx.conf
    73. worker_processes 1;
    74. events {
    75. worker_connections 1024;
    76. }
    77. http {
    78. include mime.types;
    79. default_type application/octet-stream;
    80. vhost_traffic_status_zone;
    81. log_format main '{ "@timestamp": "$time_local", '
    82. '"@fields": { '
    83. '"uri":"$request_uri",'
    84. '"url":"$uri",'
    85. '"upstream_addr":"$upstream_addr",'
    86. '"remote_addr": "$remote_addr", '
    87. '"remote_user": "$remote_user", '
    88. '"body_bytes_sent": "$body_bytes_sent", '
    89. '"host":"$host",'
    90. '"server_addr":"$server_addr",'
    91. '"request_time": "$request_time", '
    92. '"request_time":"$request_time",'
    93. '"status":"$status",'
    94. '"request": "$request", '
    95. '"request_method": "$request_method", '
    96. '"size":$body_bytes_sent,'
    97. '"upstream_time":"$upstream_response_time"'
    98. '"http_referrer": "$http_referer", '
    99. '"body_bytes_sent":"$body_bytes_sent", '
    100. '"http_x_forwarded_for": "$http_x_forwarded_for", '
    101. '"http_user_agent": "$http_user_agent" } }';
    102. sendfile on;
    103. keepalive_timeout 65;
    104. server {
    105. listen 80;
    106. server_name localhost;
    107. location / {
    108. root html;
    109. index index.html index.htm;
    110. }
    111. location /status {
    112. vhost_traffic_status_display;
    113. vhost_traffic_status_display_format html;
    114. }
    115. error_page 500 502 503 504 /50x.html;
    116. location = /50x.html {
    117. root html;
    118. }
    119. location ~ \.php$ {
    120. root html;
    121. fastcgi_pass 127.0.0.1:9000;
    122. fastcgi_index index.php;
    123. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    124. include fastcgi_params;
    125. }
    126. }
    127. }
    128. EOF
    129. systemctl restart nginx &> /dev/null
    130. if [ $? -eq 0 ];then
    131. echo "nginx重启成功"
    132. else
    133. echo "nginx重启失败"
    134. exit
    135. fi

  • 相关阅读:
    零基础转行软件测试岗位有哪些一定要注意的地方?
    9.5~10.5 GHz频段室内离体信道的测量与建模
    Java--常用类
    热重分析(TGA)真空试验中的真空度精密控制解决方案
    你能想象在亚运赛场打《王者荣耀》吗?
    ThreadLocal
    kaggle新赛:UBC卵巢癌亚型分类和异常检测大赛【图像分类】
    深入理解Linux网络笔记(七):异常TCP连接建立情况、如何查看是否有连接队列溢出发生
    细胞分离研究丨Worthington梭菌蛋白酶方案
    ESP-C3入门22. 基于VSCODE使用内置JTAG调试程序
  • 原文地址:https://blog.csdn.net/m0_62948770/article/details/126542405