Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

開啟 nginx 狀態監控並讀懂那些數字

1. 先確認模塊存在

nginx 的 ngx_http_stub_status_module 能取得自上次啟動以來的運行狀態。若是自行編譯安裝,必須在 configure 時加上 --with-http_stub_status_module 才會有這個功能。

2. 該模塊屬於 server 層級

因此必須寫在 server { } 區塊之內。

3. nginx.conf 設定

server
{
    listen       80;
    server_name  blog.xfz.com;
    index index.html index.htm index.php;
    root  /data0/htdocs/blog;

    location ~ .*\.(php|php5)?$
    {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires  30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires  1h;
    }

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 192.168.1.1;   # 設定為可訪問該狀態資訊的 IP
        deny all;
    }

    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" $http_x_forwarded_for';
    access_log /data1/logs/access.log access;
}

核心就是這一段:

location /nginx_status {
    stub_status on;
    access_log off;
    allow 192.168.1.1;
    deny all;
}

4. 重載並驗證

nginx -s reload

接著拜訪 https://blog.xfz.com/nginx_status 就能看到狀態。

5. 數字怎麼讀

Active connections: 1
server accepts handled requests
 14 14 21
Reading: 0 Writing: 1 Waiting: 0

active connections:目前 nginx 正在處理的連線數。
accepts / handled / requests:自啟動至今共接受了多少連線、成功完成多少次握手、總共處理了多少次請求;前兩者理論上相同,若出現差值即代表有連線丟失。
reading:正在讀取客戶端 Header 的數量。
writing:正在回應客戶端 Header 的數量。
waiting:開啟 keep-alive 時,等於 active − (reading + writing),也就是已處理完、正在等候下一個請求的常駐連線。

這份統計從 nginx 啟動開始累計,重載設定檔也會歸零。

另一種看法:用 netstat 統計連線狀態

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
TIME_WAIT 17
ESTABLISHED 3254
LAST_ACK 236
FIN_WAIT_1 648
FIN_WAIT_2 581
CLOSING 7
CLOSE_WAIT 4916

狀態涵義:CLOSED 無作用中連線;LISTEN 服務端等待進入呼叫;SYN_RECV 連線請求已達等待確認;SYN_SENT 應用已發起連線;ESTABLISHED 正常資料傳輸(即當前並發數);FIN_WAIT1 應用表示已完成;FIN_WAIT2 對端已同意釋放;CLOSING 雙方同時嘗試關閉;TIME_WAIT 對端已發起釋放;LAST_ACK 等待所有封包結束。CLOSE_WAIT 大量累積通常代表程式沒有把 socket 關乾淨,值得追查。

Copyright © Linux教程網 All Rights Reserved