nginx中status的使用詳解
開啟狀態(tài)界面(狀態(tài)頁(yè)面配置)
開啟status:
location /status {
stub_status {on | off};
allow 172.16.0.0/16;
deny all;
}
訪問狀態(tài)頁(yè)面的方式:http://server_ip/status
| 狀態(tài)碼 | 表示的意義 |
|---|---|
| Active connections 2 當(dāng)前所有處于打開狀態(tài)的連接數(shù) | |
| accepts | 總共處理了多少個(gè)連接 |
| handled | 成功創(chuàng)建多少握手 |
| requests | 總共處理了多少個(gè)請(qǐng)求 |
| Reading nginx | 讀取到客戶端的Header信息數(shù),表示正處于接收請(qǐng)求狀態(tài)的連接數(shù) |
| Writing nginx | 返回給客戶端的Header信息數(shù),表示請(qǐng)求已經(jīng)接收完成,且正處于處理請(qǐng)求或發(fā)送響應(yīng)的過程中的連接數(shù) |
| Waiting | 開啟keep-alive的情況下,這個(gè)值等于active - (reading + writing),意思就是Nginx已處理完正在等候下一次請(qǐng)求指令的駐留連接 |
實(shí)例
[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
#看見以下界面代表成功了

通過過濾的方式進(jìn)行監(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平臺(tái)的)
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 [::]:*
#下面是主上面進(jìn)行測(cè)試
[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é)議+用戶+密碼+主機(jī)(或者域名):prot/URI?query_args (問號(hào)前面都是)
rewrite
語(yǔ)法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; (把/images下的目錄的請(qǐng)求發(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é)果如下

如果修改了文件名測(cè)試下是否還能訪問到
[root@localhost html]# mv images/ imgs
[root@localhost html]# ls
50x.html imgs index.html index.php tset
#此行讓他找到匹配的對(duì)象
#access_log logs/host.access.log main;
location / {
root html;
index index.html;
}
location /images { #添加這句
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
}
兩種方式訪問測(cè)試:


此處的$1用于引用(.*.jpg)匹配到的內(nèi)容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
常見的flag
| flag | 作用 |
|---|---|
| last | 基本上都用這個(gè)flag,表示當(dāng)前的匹配結(jié)束,繼續(xù)下一個(gè)匹配,最多匹配10個(gè)到20個(gè) 一旦此rewrite規(guī)則重寫完成后,就不再被后面其它的rewrite規(guī)則進(jìn)行處理 而是由UserAgent重新對(duì)重寫后的URL再一次發(fā)起請(qǐng)求,并從頭開始執(zhí)行類似的過程 |
| break | 中止Rewrite,不再繼續(xù)匹配 一旦此rewrite規(guī)則重寫完成后,由UserAgent對(duì)新的URL重新發(fā)起請(qǐng)求, 且不再會(huì)被當(dāng)前l(fā)ocation內(nèi)的任何rewrite規(guī)則所檢查 |
| redirect | 以臨時(shí)重定向的HTTP狀態(tài)302返回新的URL |
| permanent | 以永久重定向的HTTP狀態(tài)301返回新的URL(開發(fā)用的比較多) |
ginx使用的語(yǔ)法源于Perl兼容正則表達(dá)式(PCRE)庫(kù),基本語(yǔ)法如下:
| 標(biāo)識(shí)符 | 意義 |
|---|---|
| ^ | 必須以^后的實(shí)體開頭 |
| $ | 必須以$前的實(shí)體結(jié)尾 |
| . | 匹配任意字符 |
| [] | 匹配指定字符集內(nèi)的任意字符 |
| [^] | 匹配任何不包括在指定字符集內(nèi)的任意字符串 |
| l | 匹配 |
| () | 分組,組成一組用于匹配的實(shí)體,通常會(huì)有 |
if
語(yǔ)法:if (condition) {…}
應(yīng)用場(chǎng)景:
server段 location段
常見的condition
變量名(變量值為空串,或者以“0”開始,則為false,其它的均為true)
以變量為操作數(shù)構(gòu)成的比較表達(dá)式(可使用=,!=類似的比較操作符進(jìn)行測(cè)試)
正則表達(dá)式的模式匹配操作
~:區(qū)分大小寫的模式匹配檢查
~*:不區(qū)分大小寫的模式匹配檢查
!~和!~*:對(duì)上面兩種測(cè)試取反
測(cè)試指定路徑為文件的可能性(-f,!-f)
測(cè)試指定路徑為目錄的可能性(-d,!-d)
測(cè)試文件的存在性(-e,!-e)
檢查文件是否有執(zhí)行權(quán)限(-x,!-x)
基于瀏覽器實(shí)現(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;
}
}
到此這篇關(guān)于nginx中status的使用詳解的文章就介紹到這了,更多相關(guān)nginx status使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Nginx反向代理與proxy_cache緩存搭建CDN服務(wù)器的配置方法
linux下通過Nginx反向代理和proxy_cache緩存搭建CDN服務(wù)器加快Web訪問速度的配置方法2013-06-06
Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景
這篇文章主要介紹了Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉(zhuǎn)詳解
訪問重寫 rewrite 是 Nginx HTTP 請(qǐng)求處理過程中的一個(gè)重要功能,下面這篇文章主要給大家介紹了Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2022-03-03
WordPress中開啟多站點(diǎn)支持及Nginx的重寫規(guī)則配置
這篇文章主要介紹了WordPress中開啟多站點(diǎn)支持及Nginx的重寫規(guī)則配置方法,在同一個(gè)WordPress軟件中開啟的多個(gè)站點(diǎn)如果需要綁定不同域名的話也可以使用WordPress MU Domain Mapping插件,需要的朋友可以參考下2016-03-03
Nginx location 和 proxy_pass路徑配置問題小結(jié)
本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,進(jìn)行測(cè)試,完全還原了整個(gè)測(cè)試過程,本文給大家介紹Nginx location 基本配置及相關(guān)配置文件,感興趣的朋友跟隨小編一起看看吧2021-09-09
Nginx實(shí)現(xiàn)http自動(dòng)跳轉(zhuǎn)到https
本文主要介紹了Nginx實(shí)現(xiàn)http自動(dòng)跳轉(zhuǎn)到https,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

