nginx监控模块

📅 发布时间:2026/7/8 21:04:21 👁️ 浏览次数:
nginx监控模块
1. ✅nginx的web监控检查下当前nginx是否有监控模块#查看nginx支持哪些模块 [rootweb01 ~]# nginx -V | grep status configure arguments: --prefix/etc/nginx --sbin-path/usr/sbin/nginx --modules-path/usr/lib64/nginx/modules --conf-path/etc/nginx/nginx.conf --error-log-path/var/log/nginx/error.log --http-log-path/var/log/nginx/access.log --pid-path/var/run/nginx.pid --lock-path/var/run/nginx.lock --http-client-body-temp-path/var/cache/nginx/client_temp --http-proxy-temp-path/var/cache/nginx/proxy_temp --http-fastcgi-temp-path/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path/var/cache/nginx/uwsgi_temp --http-scgi-temp-path/var/cache/nginx/scgi_temp --usernginx --groupnginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE2 -fexceptions -fstack-protector-strong --paramssp-buffer-size4 -grecord-gcc-switches -m64 -mtunegeneric -fPIC --with-ld-opt-Wl,-z,relro -Wl,-z,now -pie使用1.修改status配置文件 [rootweb01 ~]# vim /etc/nginx/conf.d/status.conf server { listen 80; server_name status.linux.cn; access_log off; error_log /var/log/nginx/status-error.log; location / { allow 10.0.0.1; allow 172.16.1.0/24; deny all; stub_status; } } 2.重启nginx服务 systemctl reload nginx #查看nginx状态 [rootweb01 ~]# curl -H Host:status.linux.cn http://172.16.1.7 Active connections: 4 server accepts handled requests 5 5 6 Reading: 0 Writing: 1 Waiting: 3status指令结果说明说明备注Active connections已经建立连接的数量.4层 实时数据当前网站并发访问数量accepts总数(持续增加),nginx已经接收了多少个3次握手请求绝大部分情况accepts与handlers一致类似handled总数(持续增加),nginx已经处理的请求数量requests总数(持续增加),nginx已经收到了多少http请求数量比accepts数量多.(连接建立后,连接可以重复使用)Reading实时数据.正在读取客户端请求(头,主体)数量Writing实时数据.正在返回客户端响应(头,主体)数量Waiting实时数据.空闲的连接数量.(3次握手建立连接,发出http请求,连接保留,此时没有请求使用连接)压力测试ab -H Host:lb.linux.cn -n 999999 -c 10 -k http://10.0.0.7/ -H 修改请求头信息 -n 请求数量 -c 并发数量 -k 启动长连接 ab结果 Percentage of the requests served within a certain time (ms) 50% 0 66% 1 75% 1 80% 1 90% 1 #90%的请求的时间1ms 95% 1 98% 2 99% 3 100% 44 (longest request)2. ✅nginx负载监控第三方模块nginx没有安装,nginx官方开源也没有,nginx官方商业版有2.1. upstream_check模块下载这个模块的代码并且编译安装nginx准备使用tengine实现(代码中包含常用的nginx第3方模块)既要编译安装tengine,又要保持与yum安装的一致性编译安装nginx/tengine--nginx命令1.下载tengine代码 wget https://tengine.taobao.org/download/tengine-3.1.0.tar.gz 2.安装依赖 yum install -y openssl-devel pcre-devel 3.解压 tar xf tengine-3.1.0.tar.gz 4.配置 ./configure --prefix/etc/nginx --sbin-path/usr/sbin/nginx --modules-path/usr/lib64/nginx/modules --conf-path/etc/nginx/nginx.conf --error-log-path/var/log/nginx/error.log --http-log-path/var/log/nginx/access.log --pid-path/var/run/nginx.pid --lock-path/var/run/nginx.lock --http-client-body-temp-path/var/cache/nginx/client_temp --http-proxy-temp-path/var/cache/nginx/proxy_temp --http-fastcgi-temp-path/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path/var/cache/nginx/uwsgi_temp --http-scgi-temp-path/var/cache/nginx/scgi_temp --usernginx --groupnginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE2 -fexceptions -fstack-protector-strong --paramssp-buffer-size4 -grecord-gcc-switches -m64 -mtunegeneric -fPIC --with-ld-opt-Wl,-z,relro -Wl,-z,now -pie \ --add-module./modules/ngx_http_upstream_check_module \ --add-module./modules/ngx_http_upstream_session_sticky_module 5.编译 make -j nproc 6.传输 objs/nginx 到lb01 scp objs/nginx root172.16.1.7:~ 7.备份旧命令然后更新nginx mv /sbin/nginx /sbin/nginx-1.26 8.重启Nginx systemctl restart nginx 9.curl检查 curl -v http://10.0.0.7/ 有Tengine即可2.2. 配置使用模块展示负载均衡分组中,节点的可用情况使用tcp仅仅检查后端节点的端口端口是否可以访问check interval3000 rise2 fall5 timeout1000 typetcp port80; interval3000 3000ms3m每隔3秒检查1 次. rise2 成功2次及以上认为节点可用. fall5 失败5次及以上认为节点不可用. typehttp; 类型 http或tcp port80使用http协议检查使用浏览器访问(curl)指定Host,指定URI,指定UA,更加接近与用户访问.upstream lb_pool { server 10.0.0.7:80 weight1; server 10.0.0.8:80 weight1; check interval3000 rise2 fall5 timeout1000 typehttp; check_http_send HEAD / HTTP/1.0\r\nHost: lb.linux.cn\r\nUser-Agent: lb_check\r\n\r\n; check_http_expect_alive http_2xx http_3xx; }监控lb站点即可lb01配置文件(nginx替换成tengine后)web01、web02配置文件及目录、用户要一致upstream lb_pool { server 10.0.0.7:80 weight1; server 10.0.0.8:80 weight1; check interval3000 rise2 fall5 timeout1000 typehttp; check_http_send HEAD / HTTP/1.0\r\nHost: lb.linux.cn\r\nUser-Agent: lb_check\r\n\r\n; check_http_expect_alive http_2xx http_3xx; } server { listen 80; server_name lb.linux.cn; access_log /var/log/nginx/lb-access.log main; error_log /var/log/nginx/lb-error.log notice; location / { proxy_pass http://lb_pool; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-Ip $remote_addr; } location /lb_status { allow 10.0.0.1; allow 172.16.1.0/24; deny all; check_status; } location /ngx_status { allow 10.0.0.1; allow 172.16.1.0/24; deny all; stub_status; } }2.3. 参数详解参数含义interval3000检查间隔为 3 秒单位毫秒rise2请求成功多少次后标记为正常fall3请求失败多少次后标记为异常timeout1000单次请求超时时间单位毫秒typehttp使用 HTTP 类型检查也可以是 tcp、ssl_hello、mysql、ajp 等check_http_send发送的 HTTP 请求内容check_http_expect_status期望返回的状态码范围2.4. 浏览器查看3. ✅nginx平滑升级使用restart重启,升级会导致一些已建立的连接断开1.备份旧的nginx命令 mv /sbin/nginx /sbin/nginx-1.26 2.拷贝旧的nginx命令到/sbin/目录下 mv nginx /sbin/nginx 3.使用kill -USR2 pid(旧的),保持旧的命令的进程不关,用新的命令启动新的进程 kill -USR2 pid 4.过一段时间使用kill命令将旧的进程关闭 kill pid(旧的)4. ✅跳转功能/模块server { listen 80; server_name baidu.linux.cn; return 301 http://www.baidu.com$request_uri; }rewrite模块核心指令说明应用场景return指令URL跳转301302跳转更加简单,一般使用ngx变量rewrite指令URL跳转复杂跳转,需要使用正则匹配setngx创建变量创建或修改变量if判断非uri的内容(ngx中的if只有简单分支)跳转何时使用新旧域名www.360buy.com -- www.jd.comhttp--https伪静态运维人员,搜索引擎喜欢收集静态页面,动态页面搜索引擎不喜欢.动态页面 --静态 urlhttps://search.jd.com/Search? keywordssdencutf- 8wqssdpvid0984120ca84642988f1b647de07d8 537 ? 动态请求 https://search.jd.com/lidao.html实现新老域名跳转(或http--https)re.linux.cn ---www.baidu.com 302 临时给网站设置1个开关,网站是否处于维护模式503.方法1: 子配置文件创建变量,flag等于1或0.人工修改. 如果变量为1则return 503. # set $flag 1; # if ( $flag 1 ) { # return 503; # } 方法2: 子配置文件加入判断文件是否存在weihu.html 文件存在则输出503. if ( -f /etc/nginx/weihu.txt ) { return 503; } [rootweb01 /etc/nginx/conf.d]# cat re.linux.cn.conf server { listen 80; server_name re.linux.cn; root /app/code/re; # set $flag 1; # if ( $flag 1 ) { # return 503; # } if ( -f /etc/nginx/weihu.txt ) { return 503; } location / { index index.html; } }网站只准许GET,POST,HEAD请求,其他请求方法都拒绝405[rootweb01 /etc/nginx/conf.d]# cat re.linux.cn.conf server { listen 80; server_name re.linux.cn; #set $flag 1; if ( $request_method !~^(GET|POST|HEAD)$ ) { return 405; } set $file /etc/nginx/weihu.html; if ( -f $file ) { return 503; } root /app/code/re; location / { index index.html; } } curl -X PUT -H Host:re.linux.cn http://10.0.0.7 $sent_http_响应头内容 $http_请求头 小写,下划线. $request_method 请求方法 $request_uri 请求uri $status 状态码 $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;