nginx中status的使用詳解
開啟狀態(tài)界面(狀態(tài)頁面配置)
開啟status:
location /status {
stub_status {on | off};
allow 172.16.0.0/16;
deny all;
}
訪問狀態(tài)頁面的方式:http://server_ip/status
| 狀態(tài)碼 | 表示的意義 |
|---|---|
| Active connections 2 當前所有處于打開狀態(tài)的連接數(shù) | |
| accepts | 總共處理了多少個連接 |
| handled | 成功創(chuàng)建多少握手 |
| requests | 總共處理了多少個請求 |
| Reading nginx | 讀取到客戶端的Header信息數(shù),表示正處于接收請求狀態(tài)的連接數(shù) |
| Writing nginx | 返回給客戶端的Header信息數(shù),表示請求已經(jīng)接收完成,且正處于處理請求或發(fā)送響應的過程中的連接數(shù) |
| 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
#看見以下界面代表成功了

通過過濾的方式進行監(jiān)控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監(jiān)控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組成:
協(xié)議+用戶+密碼+主機(或者域名):prot/URI?query_args (問號前面都是)
rewrite
語法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; (把/images下的目錄的請求發(fā)給 /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
直接查找是可以訪問到的,結(jié)果如下

如果修改了文件名測試下是否還能訪問到
[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)匹配到的內(nèi)容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
常見的flag
| flag | 作用 |
|---|---|
| last | 基本上都用這個flag,表示當前的匹配結(jié)束,繼續(xù)下一個匹配,最多匹配10個到20個 一旦此rewrite規(guī)則重寫完成后,就不再被后面其它的rewrite規(guī)則進行處理 而是由UserAgent重新對重寫后的URL再一次發(fā)起請求,并從頭開始執(zhí)行類似的過程 |
| break | 中止Rewrite,不再繼續(xù)匹配 一旦此rewrite規(guī)則重寫完成后,由UserAgent對新的URL重新發(fā)起請求, 且不再會被當前l(fā)ocation內(nèi)的任何rewrite規(guī)則所檢查 |
| redirect | 以臨時重定向的HTTP狀態(tài)302返回新的URL |
| permanent | 以永久重定向的HTTP狀態(tài)301返回新的URL(開發(fā)用的比較多) |
ginx使用的語法源于Perl兼容正則表達式(PCRE)庫,基本語法如下:
| 標識符 | 意義 |
|---|---|
| ^ | 必須以^后的實體開頭 |
| $ | 必須以$前的實體結(jié)尾 |
| . | 匹配任意字符 |
| [] | 匹配指定字符集內(nèi)的任意字符 |
| [^] | 匹配任何不包括在指定字符集內(nèi)的任意字符串 |
| l | 匹配 |
| () | 分組,組成一組用于匹配的實體,通常會有 |
if
語法:if (condition) {…}
應用場景:
server段 location段
常見的condition
變量名(變量值為空串,或者以“0”開始,則為false,其它的均為true)
以變量為操作數(shù)構(gòu)成的比較表達式(可使用=,!=類似的比較操作符進行測試)
正則表達式的模式匹配操作
~:區(qū)分大小寫的模式匹配檢查
~*:不區(qū)分大小寫的模式匹配檢查
!~和!~*:對上面兩種測試取反
測試指定路徑為文件的可能性(-f,!-f)
測試指定路徑為目錄的可能性(-d,!-d)
測試文件的存在性(-e,!-e)
檢查文件是否有執(zhí)行權限(-x,!-x)
基于瀏覽器實現(xiàn)分離案例
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使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
nginx配置SSL證書實現(xiàn)https服務的方法
這篇文章主要介紹了nginx配置SSL證書實現(xiàn)https服務的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
nginx內(nèi)部訪問特性如何實現(xiàn)靜態(tài)資源授權訪問
這篇文章主要介紹了nginx內(nèi)部訪問特性如何實現(xiàn)靜態(tài)資源授權訪問方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

