• Nginx网络服务之监控模块


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


    前言


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、Nginx服务基础

    1、关于Nginx——一款高性能、轻量级Web服务软件

    稳定性高
    系统资源消耗低
    对HTTP并发连接的处理能力高
    单台物理服务器可支持30000 ~ 50000个并发请求(理论情况下)实际情况下大概20000 ~ 30000左右

    2、上传nginx-module-vst-master软件包并解压

    unzip nginx-module-vts-master.zip
    mv nginx-module-vts-master /usr/local/
    
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    在这里插入图片描述

    3、安装Nginx

    #yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make
    
    
    • 1
    • 2

    在这里插入图片描述

    (1)、编译安装nginx

    上传nginx-1.15.9.tar.gz到/opt目录下
    在这里插入图片描述
    在这里插入图片描述

    cd /opt
    tar -zxvf nginx-1.15.9.tar.gz
    
    cd nginx-1.15.9/
    ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --add-module=/usr/local/nginx-module-vts-master
    
    make -j4 && make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    (2)、优化管理

    useradd -M -s /sbin/nologin nginx
    nginx -t   #检查配置文件是否配置正确
    nginx  #启动
    
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    在这里插入图片描述

    (3)、添加Nginx系统服务

    vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=nginx
    After=network.target
    [Service]
    Type=forking
    PIDFile =/usr/local/nginx/logs/nginx.pid
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.targe
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    [Unit]	
    Description=nginx							#描述
    After=network.target						#描述服务类别
    [Service]
    Type=forking								#后台运行类型
    PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置
    ExecStart=/usr/local/nginx/sbin/nginx		#启动服务
    ExecrReload=/bin/kill -s HUP $MAINPID		#根据PID重载配置
    ExecrStop=/bin/kill -s QUIT $MAINPID		#根据PID终止进程
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target					#启动级别
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    chmod 754 /usr/lib/systemd/system/nginx.service		#设置754权限是一种安全优化
    systemctl start nginx.service
    systemctl enable nginx.service
    
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    (4)、备份

    cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
    vim /usr/local/nginx/conf/nginx.conf
    
    
    • 1
    • 2
    • 3
    #user  nobody; #默认运行/管理用户
    worker_processes  1; #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1
    
    #error_log  logs/error.log; #错误日志文件路径/级别
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid; #pid文件位置
    
    
    events {            #events:事件
        #use epoll      #默认是没写的,epoll是抗高并发的参数之一
        worker_connections  1024; #每个进程最多处理的连接数量(socket)上限是65535
        PS:两种修改方式,以下是临时
    #如提高每个进程的连接数还需执行“ulimit -n 65535”(临时调整)命令临时修改本地每个进程可以同时打开的最大文件数。
    #在Linux 平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制
    #可使用ulimit -a 命令查看系统允许当前用户进程打开的文件数限制。
    }
    
    
    http {      #http协议的配置
        include       mime.types;  #文件拓展名与文件类型映射表
        default_type  application/octet-stream;  #默认文件类型
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' #日志格式设置
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;  #访问日志位置
    
        sendfile        on;  #支持文件发送(下载)
        #tcp_nopush     on;  #此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
        
        #keepalive_timeout  0;  ##连接保持超时时间,单位:秒
        keepalive_timeout  65;
    
        #gzip  on; #压缩模块 on 表示开启
    
        server {   #web服务相关的一些配置
            listen       80;   #默认的监听端口
            server_name  localhost; #站点域名,可以修改域名,例如www.liuxu.com
    
            #charset koi8-r;   #字符集支持(修改为中文)UTF-8
    
            #access_log  logs/host.access.log  main; #此web服务的主访问日志
    
            location / {  #/根目录配置,(浏览器中,www.baidu.com/,访问的根路径
                root   html; #网站根目录的位置/usr/local/nginx/html(相对路径)
                index  index.html index.htm; #支持的首页文件格式
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html #当发生错误的时候能够显示一个预定义的错误页面
            #
            error_page   500 502 503 504  /50x.html; #当发生错误的时候能够显示一个预定义的错误页面
            location = /50x.html {       #错误页面配置
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80 #以下是支持PHP及跳转的配置
            #
            #location ~ \.php$ { 
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {  #虚拟主机的配置文件
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server  #HTTPS的配置(SSL 加密)
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    在这里插入图片描述

    Nginx配置文件管控的层次结构
    1、gloabl:全局生效的配置
    2、协议http { } nginx主要控制的就是HTTP层面
    3、server { } 具体匹配进入nginx的相关配置
    4、location 主要用于匹配URL并基于匹配到的URL做相关处理
    
    • 1
    • 2
    • 3
    • 4

    4、Nginx监控

    (1)、监控Nginx主要用到以下三个模块:

    1、nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。
    2、nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号99133、Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。
    
    • 1
    • 2
    • 3

    (2)、修改配置文件

    #PS:主要是修改默认日志文件格式,添加压缩配置,添加监控配置,具体可根据自己的需求修改
    
    • 1
    http {
        include       mime.types;
        default_type  application/octet-stream;
        vhost_traffic_status_zone;			#流量状态监控
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    PS:开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。
    假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现“127.0.0.1”,hostname等的域名监控信息
    
    
    • 1
    • 2
    • 3
    log_format main '{ "@timestamp": "$time_local", '
    '"@fields": { '
    '"uri":"$request_uri",'
    '"url":"$uri",'
    '"upstream_addr":"$upstream_addr",'
    '"remote_addr": "$remote_addr", '
    '"remote_user": "$remote_user", '
    '"body_bytes_sent": "$body_bytes_sent", '
    '"host":"$host",'
    '"server_addr":"$server_addr",'
    '"request_time": "$request_time", '
    '"request_time":"$request_time",'
    '"status":"$status",'
    '"request": "$request", '
    '"request_method": "$request_method", '
    '"size":$body_bytes_sent,'
    '"upstream_time":"$upstream_response_time"'
    '"http_referrer": "$http_referer", '
    '"body_bytes_sent":"$body_bytes_sent", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"http_user_agent": "$http_user_agent" } }';
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    server {
     ..............
             location / {
                root   html;
                index  index.html index.htm;
            }
            location /status {
                vhost_traffic_status_display;
                vhost_traffic_status_display_format html;
            }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    (3)、启动

    /usr/local/nginx/sbin/nginx
    
    
    • 1
    • 2

    在这里插入图片描述

    (4)、访问测试(首页)——http://192.168.116.10

    在这里插入图片描述

    (5)、访问状态监控模块——http://192.168.116.10/status

    在这里插入图片描述
    不停刷新可以看到请求的数值、成功连接数量等数值在变化
    在这里插入图片描述
    在这里插入图片描述


  • 相关阅读:
    大数据基础设施搭建 - JDK
    QT下,如何获取控制台输入
    koltin 泛型Any和*的区别
    LeetCode(36)旋转图像【矩阵】【中等】
    springboot16:指标监控(线上指标监控,微服务,高级特性)
    数组c++介绍
    JS高阶:深入理解数据、变量、内存
    CDH大数据平台 Error: Package: 1:mariadb-devel-5.5.68-1.el7.x86_64 (base)
    最新版Spring Security & Spring Session 实现单点登录
    电脑桌面文件不见了怎么恢复?
  • 原文地址:https://blog.csdn.net/zhou641694375/article/details/126577161