1. 程式人生 > 實用技巧 >Nginx配置及status監控

Nginx配置及status監控

nginx的平滑升級(熱部署)

Nginx方便地幫助我們實現了平滑升級。其原理簡單概括,就是:

(1)在不停掉老程序的情況下,啟動新程序。

(2)老程序負責處理仍然沒有處理完的請求,但不再接受處理請。

(3)新程序接受新請求。

(4)老程序處理完所有請求,關閉所有連線後,停止。

熱部署載入echo模組

  • 檢視現有已編譯模組
[root@localhost ~]# nginx -V
.......
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
  • 下載echo模組程式碼
[root@localhost ~]# ls
anaconda-ks.cfg  cowsay.sh  echo-nginx-module-master.zip

## 解壓zip包
[root@localhost ~]# unzip echo-nginx-module-master.zip
[root@localhost ~]# ls
anaconda-ks.cfg  cowsay.sh  echo-nginx-module-master  echo-nginx-module-master.zip

## 備份原nginx程式
[root@localhost sbin]# cp nginx nginx.bak
[root@localhost sbin]# ls
nginx  nginx.bak

## 編譯新nginx
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master  ##新增新的模組

[root@localhost nginx-1.18.0]# make
......

## 檢視新的nginx啟動程式包含模組
[root@localhost nginx-1.18.0]# cd objs/
[root@localhost objs]# ./nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master  ##已經成功編譯新模組


## 停止舊程序並複製nginx程式,最後執行新程式
[root@localhost objs]# nginx -s stop ;cp ./nginx /usr/local/nginx/sbin/ ; /usr/local/nginx/sbin/nginx
cp: overwrite '/usr/local/nginx/sbin/nginx'? yes
  • 檢視程序並測試新模組
[root@localhost objs]# ss -antl
State   Recv-Q   Send-Q       Local Address:Port                    Peer Address:Port                 
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 nginx]# vim conf/nginx.conf
......
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html;
            index  index.php index.html index.htm ;
        }

        location /test {
            echo "HELLO WORLD"    訪問/test列印HELLOWORLD
        }

[root@localhost nginx]# nginx -s reload


## 測試訪問
[root@localhost nginx]# curl 192.168.197.141/test
HELLO WORLD

location配置段

location語法規則

location [=|~|~*|^~] /uri/ { … }

location修飾符

符號 含義
= 表示精確匹配
^~ 表示uri以某個常規字串開頭,理解為匹配 url路徑即可。
~ 表示區分大小寫的正則匹配
~* 表示不區分大小寫的正則匹配

修飾符優先順序

當有多條 location 規則時,nginx 有一套比較複雜的規則,優先順序如下:

  • 精確匹配 =
  • 字首匹配 ^~(立刻停止後續的正則搜尋)
  • 按檔案中順序的正則匹配 ~ 或 ~*
  • 匹配不帶任何修飾的字首匹配

訪問控制

用於location/server/httpd段

  • allow:設定允許哪臺或哪些主機訪問,多個引數間用空格隔開
  • deny:設定禁止哪臺或哪些主機訪問,多個引數間用空格隔開

例項:

location /test {
            echo "HELLO WORLD";
            allow 192.168.197.141;   允許該ip訪問伺服器
            deny all;               拒絕別的主機訪問改伺服器
        }

[root@localhost nginx]# nginx -s reload


## 192.168.197.1主機訪問不了
C:\Users\Sawyer>curl 192.168.197.141/test
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

## 192.168.197.141 主機訪問成功
[root@localhost nginx]# curl 192.168.197.141/test
HELLO WORLD

使用者認證

通過登入使用者名稱密碼的方式進行訪問nginx伺服器

語法: htpasswd -c -m /path/to/.user_auth_file USERNAME

  • 例項:
[root@localhost nginx]# yum install -y httpd-tools


## 新增虛擬認證使用者及密碼
[root@localhost nginx]# htpasswd -c -m /root/.root_passwd root
New password: 
Re-type new password: 
Adding password for user root

## 編輯配置檔案
 location /test {
            echo "HELLO WORLD";
            auth_basic "你好";
            auth_basic_user_file "/root/.root_passwd
        }
[root@localhost ~]# nginx -s reload
  • 測試訪問

Zabbix自定義監控web狀態

伺服器名 IP
Nginx伺服器 192.168.197.141
Zabbix伺服器 192.168.197.138
  • Nginx伺服器段安裝zabbix客戶端,並修改配置檔案
[root@localhost ~]# tar -xf zabbix-5.2.0.tar.gz -C /usr/src
[root@localhost ~]# cd usr/src
-bash: cd: usr/src: No such file or directory
[root@localhost ~]# cd /usr/src
[root@localhost src]# ls
debug    mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  nginx-1.18.0.tar.gz
kernels  nginx-1.18.0                                zabbix-5.2.0
[root@localhost src]# cd zabbix-5.2.0/

## 編譯安裝
[root@localhost zabbix-5.2.0]# ./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.2.0]# make install
......

## 配置zabbix_agentd配置檔案
[root@localhost etc]# vim zabbix_agentd.conf
......
# Server=

Server=192.168.197.138    指定伺服器IP

......

ServerActive=192.168.197.138  

......

Hostname=nginx_server
  • 開啟zabbix_agentd
[root@localhost etc]# useradd -r -m -s /sbin/nologin zabbix
[root@localhost etc]# zabbix_agentd 
[root@localhost etc]# ss -antl
State    Recv-Q    Send-Q          Local Address:Port          Peer Address:Port    
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                   0.0.0.0:10050              0.0.0.0:*       
LISTEN   0         128                 127.0.0.1:9000               0.0.0.0:*       
LISTEN   0         128                      [::]:22                    [::]:*       
LISTEN   0         80                          *:3306                     *:*  
  • Zabbix_server端WEB頁新建主機

  • nginx伺服器新建狀態頁面
location / {
            root   /usr/local/nginx/html;
            index  index.php index.html index.htm ;
        }
        location /status {
            stub_status on;
        }


## 測試訪問
[root@localhost conf]# curl 192.168.197.141/status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0
  • 編寫專案監控指令碼
[root@localhost scripts]# !vim
vim nginx_status.sh 

#!/bin/bash
  
Active_con=`curl -s 192.168.197.141/status |grep Active|awk '{print $3}'`
accepts=`curl -s 192.168.197.141/status |awk NR==3 |awk '{print $1}'`
handled=`curl -s 192.168.197.141/status |awk NR==3 |awk '{print $2}'`
requests=`curl -s 192.168.197.141/status |awk NR==3 |awk '{print $3}'`
reading=`curl -s 192.168.197.141/status |grep Reading|awk '{print $2}'`
writing=`curl -s 192.168.197.141/status |grep Reading|awk '{print $4}'`
waiting=`curl -s 192.168.197.141/status |grep Reading|awk '{print $6}'`

case $1 in
active_con )
        echo $Active_con
        ;;
accepts )
        echo $accepts
        ;;
handled )
        echo $handled
        ;;
requests )
        echo $requests
        ;;
reading )
        echo $reading
        ;;
writing )
        echo $writing
        ;;
waiting )
        echo $waiting
        ;;
* )
        echo 'please enter (active_con,accepts,handled,requests,reading,writing,waiting) parameters'
        ;;
esac
  • 將指令碼寫入zabbix_agentd配置檔案
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
# Range: 0-1
# Default:
UnsafeUserParameters=1 ## 開啟自定義監控項引數
......
 TLSCipherAll=
UserParameter=nginx_status[*],/scripts/nginx_status.sh $1  ## 檔案末尾寫入

## 重啟zabbix
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd 


## zabbix伺服器上測試指令碼
[root@localhost ~]# zabbix_get -s 192.168.197.141 -k 'nginx_status[writing]'
1
[root@localhost ~]# zabbix_get -s 192.168.197.141 -k 'nginx_status[ssss]'
please enter (active_con,accepts,handled,requests,reading,writing,waiting) parameters
  • zabbix Web頁新增item

注意:如果需要將web_status中的每個指標監控,則需要新建多個item,這裡以"writing"為示例

  • 成功取到writing值.

  • 新建觸發告警