nginx中status的使用详解

 更新时间:2026年01月16日 09:39:25   作者:早川语  
本文主要介绍了nginx中status的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

开启状态界面(状态页面配置)

开启status:

location /status {
  stub_status {on | off};
  allow 172.16.0.0/16;
  deny all;
}

访问状态页面的方式:http://server_ip/status

状态码表示的意义
Active connections 2 当前所有处于打开状态的连接数
accepts总共处理了多少个连接
handled成功创建多少握手
requests总共处理了多少个请求
Reading nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数
Writing nginx返回给客户端的Header信息数,表示请求已经接收完成,且正处于处理请求或发送响应的过程中的连接数
Waiting开启keep-alive的情况下,这个值等于active - (reading + writing),意思就是Nginx已处理完正在等候下一次请求指令的驻留连接

实例

[root@localhost conf]# vim nginx.conf

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html;
        }

        location /status {
            stub_status;
        }
[root@localhost conf]# nginx

#看见以下界面代表成功了

通过过滤的方式进行监控status

[root@localhost conf]# curl http:/192.168.230.132/status
Active connections: 1 
server accepts handled requests
 3 3 3 
Reading: 0 Writing: 1 Waiting: 0 
[root@localhost conf]# curl -s http:/192.168.230.132/status |awk 'NR==4'
Reading: 0 Writing: 1 Waiting: 0 
[root@localhost conf]# curl -s http:/192.168.230.132/status |awk 'NR==4{print $2}'
0

使用zabbix监控status

#先在从安装下zabbix
[root@localhost src]# cd /usr/local/
[root@localhost local]# tar xf zabbix-5.4.4.tar.gz 
[root@localhost local]# cd zabbix-5.4.4
[root@localhost zabbix-5.4.4]# useradd -r -M -s /sbin/nologin zabbix
[root@localhost zabbix-5.4.4]# yum -y install vim wget gcc gcc-c++ make pcre-devel openssl openssl-devel
[root@localhost zabbix-5.4.4]# ./configure --enable-agent
  LDAP support:          no
  IPv6 support:          no

***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************
[root@localhost zabbix-5.4.4]# make install
[root@localhost ~]# tr -dc A-Za-z < /dev/urandom | head -c 8 |xargs
sbncsVLR
[root@localhost ~]# cd /usr/local/etc/
[root@localhost etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@localhost etc]# vim zabbix_agentd.conf
(注意:这里配置113行和154行的IP是控制端上的,也就是拥有zabbix平台的)

113 Server=192.168.230.131
154 ServerActive=192.168.230.131
165 Hostname=sbncsVLR
322 UnsafeUserParameters=1     #值修改为1
525 UserParameter=check_status,/scripts/check_status.sh     

[root@localhost etc]# zabbix_agentd 

#编写脚本
[root@localhost scripts]# vim check_status.sh 
[root@localhost scripts]# cat check_status.sh 
#!/bin/bash

check_status=$(curl -s 192.168.230.132/status |awk 'NR==4'|awk -F: {'print $4'})

if [ $check_status -ge 1 ];then
    echo 1
else
    echo 0
fi


[root@localhost scripts]# chmod +x check_status.sh 
[root@localhost scripts]# ll
总用量 4
-rwxr-xr-x 1 root root 128 10月 28 08:47 check_status.sh

[root@localhost conf]# pkill zabbix-agent
[root@localhost conf]# ss -anlt
State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:10050        0.0.0.0:*            
LISTEN 0      128          0.0.0.0:80           0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22           0.0.0.0:*            
LISTEN 0      128             [::]:22              [::]:*    

#下面是主上面进行测试
[root@localhost ~]# ss -anlt
State   Recv-Q  Send-Q    Local Address:Port      Peer Address:Port  
LISTEN  0       128           127.0.0.1:9000           0.0.0.0:*     
LISTEN  0       128             0.0.0.0:111            0.0.0.0:*     
LISTEN  0       128             0.0.0.0:80             0.0.0.0:*     
LISTEN  0       32        192.168.122.1:53             0.0.0.0:*     
LISTEN  0       128             0.0.0.0:22             0.0.0.0:*     
LISTEN  0       5             127.0.0.1:631            0.0.0.0:*     
LISTEN  0       128             0.0.0.0:10050          0.0.0.0:*     
LISTEN  0       128             0.0.0.0:10051          0.0.0.0:*     
LISTEN  0       80                    *:3306                 *:*     
LISTEN  0       128                [::]:111               [::]:*     
LISTEN  0       128                [::]:22                [::]:*     
LISTEN  0       5                 [::1]:631               [::]:*     
[root@localhost ~]# zabbix_get -s 192.168.230.132 -k check_status
0

[root@localhost ~]# zabbix_get -s 192.168.230.132 -k check_status
1

rewrite URL重定向

URL组成:

协议+用户+密码+主机(或者域名):prot/URI?query_args (问号前面都是)

rewrite
语法:rewrite regex replacement flag;,如:

rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
(把/images下的目录的请求发给 /imgs)
[root@localhost conf]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html  index.html  index.php  tset
[root@localhost html]# mkdir images
[root@localhost html]# cd images/
[root@localhost images]# ls
#随便找张照片放上去
[root@localhost images]# ls
[root@localhost images]# ls
0075TGutgy1gvuz5ma0hhj30l816utg4.jpg
[root@localhost images]# mv 0075TGutgy1gvuz5ma0hhj30l816utg4.jpg 1.jpg
[root@localhost images]# ls
1.jpg

直接查找是可以访问到的,结果如下

如果修改了文件名测试下是否还能访问到
[root@localhost html]# mv images/ imgs
[root@localhost html]# ls
50x.html  imgs  index.html  index.php  tset

 #此行让他找到匹配的对象
    
       #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html;
        }

        location /images {         #添加这句
			rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
        }

两种方式访问测试:

此处的$1用于引用(.*.jpg)匹配到的内容,又如:

rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;

常见的flag

flag作用
last基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个
一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理
而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break中止Rewrite,不再继续匹配
一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求,
且不再会被当前location内的任何rewrite规则所检查
redirect以临时重定向的HTTP状态302返回新的URL
permanent以永久重定向的HTTP状态301返回新的URL(开发用的比较多)

ginx使用的语法源于Perl兼容正则表达式(PCRE)库,基本语法如下:

标识符意义
^必须以^后的实体开头
$必须以$前的实体结尾
.匹配任意字符
[]匹配指定字符集内的任意字符
[^]匹配任何不包括在指定字符集内的任意字符串
l匹配
()分组,组成一组用于匹配的实体,通常会有

if

语法:if (condition) {…}

应用场景:

server段
location段

常见的condition

变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
正则表达式的模式匹配操作

~:区分大小写的模式匹配检查
~*:不区分大小写的模式匹配检查
!~和!~*:对上面两种测试取反
测试指定路径为文件的可能性(-f,!-f)
测试指定路径为目录的可能性(-d,!-d)
测试文件的存在性(-e,!-e)
检查文件是否有执行权限(-x,!-x)

基于浏览器实现分离案例

if ($http_user_agent ~ Firefox) {
  rewrite ^(.*)$ /firefox/$1 break;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
}

if ($http_user_agent ~ Chrome) {
  rewrite ^(.*)$ /chrome/$1 break;
}

防盗链案例

location ~* \.(jpg|gif|jpeg|png)$ {
  valid_referers none blocked www.idfsoft.com;
  if ($invalid_referer) {
    rewrite ^/ http://www.idfsoft.com/403.html;
  }
}

到此这篇关于nginx中status的使用详解的文章就介绍到这了,更多相关nginx status使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • nginx安全防护与https部署全过程

    nginx安全防护与https部署全过程

    这篇文章主要介绍了nginx安全防护与https部署全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-05-05
  • Nginx暴露出请求的真实IP的问题

    Nginx暴露出请求的真实IP的问题

    在工作中,经常会用用户实际请求的IP地址,本文主要介绍了Nginx暴露出请求的真实IP的问题,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • 使用Nginx反向代理与proxy_cache缓存搭建CDN服务器的配置方法

    使用Nginx反向代理与proxy_cache缓存搭建CDN服务器的配置方法

    linux下通过Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度的配置方法
    2013-06-06
  • Nginx Rewrite模块应用的几种场景

    Nginx Rewrite模块应用的几种场景

    这篇文章主要介绍了Nginx Rewrite模块应用的几种场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Nginx中location匹配以及rewrite重写跳转详解

    Nginx中location匹配以及rewrite重写跳转详解

    访问重写 rewrite 是 Nginx HTTP 请求处理过程中的一个重要功能,下面这篇文章主要给大家介绍了Nginx中location匹配以及rewrite重写跳转的相关资料,需要的朋友可以参考下
    2022-03-03
  • WordPress中开启多站点支持及Nginx的重写规则配置

    WordPress中开启多站点支持及Nginx的重写规则配置

    这篇文章主要介绍了WordPress中开启多站点支持及Nginx的重写规则配置方法,在同一个WordPress软件中开启的多个站点如果需要绑定不同域名的话也可以使用WordPress MU Domain Mapping插件,需要的朋友可以参考下
    2016-03-03
  • Nginx location 和 proxy_pass路径配置问题小结

    Nginx location 和 proxy_pass路径配置问题小结

    本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,进行测试,完全还原了整个测试过程,本文给大家介绍Nginx location 基本配置及相关配置文件,感兴趣的朋友跟随小编一起看看吧
    2021-09-09
  • Nginx实现http自动跳转到https

    Nginx实现http自动跳转到https

    本文主要介绍了Nginx实现http自动跳转到https,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • nginx配置报404问题排查解决

    nginx配置报404问题排查解决

    这篇文章主要给大家介绍了关于nginx配置报404问题排查解决的相关资料,当我们在访问网站时常常会遇到一些我们没能想到的问题或者其他错误,此时我们访问的是无法路由的页面,也就是404页面,需要的朋友可以参考下
    2023-08-08
  • nginx 匹配规则小总结(推荐)

    nginx 匹配规则小总结(推荐)

    这篇文章主要介绍了nginx 匹配规则小总结及nginx配置proxy_pass路径带/问题,需要的朋友可以参考下
    2018-07-07

最新评论