nginx配置監控


 

 

通過查看Nginx的並發連接,我們可以更清除的知道網站的負載情況。Nginx並發查看有兩種方法(之所以這么說,是因為筆者只知道兩種),一種是通過web界面,一種是通過命令,web查看要比命令查看顯示的結果精確一些。下面介紹這兩種查看方法;

1.通過瀏覽器查看

  通過web界面查看時Nginx需要開啟status模塊,也就是安裝Nginx時加上        –with-http_stub_status_module   然后配置Nginx.conf,在server點里面加入如下內容。

獲取 Nginx 狀態( http_stub _status )

[root@localhost nginx]# 
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module

  

##  查看編譯時有沒有加入狀態監控模塊,如果沒有需要單獨加載

1)編譯添加http_stub _status

[root@localhost local]# cd nginx-1.8.0
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
[root@localhost nginx-1.8.0]# make && make install
 
查看已安裝的 Nginx 是否包含 stub_status 模塊
[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx -V

  

ok,接着配置nginx.conf

[root@localhost local]# vi nginx/conf/nginx.conf
 
    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        #location / {
           #root   html;
           #index  index.html index.htm;
        #}
 
       location ~ /nginx_status {
          root   html;
          index  index.html index.htm;
          stub_status on; //#打開目錄瀏覽功能
          access_log off; //#默認為on,顯示出文件的確切大小,單位是bytes #顯示出文件的大概大小,單位是kB或者MB或者GB
          allow 127.0.0.1; //訪問ip
          allow 192.168.0.103; //訪問ip
          deny all;
       }  

  

or 通用nginx.conf配置;

user www www;
worker_processes 2;
  
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
  
pid logs/nginx.pid;
  
  
events {
use epoll;
worker_connections 2048;
}
  
  
http {
include mime.types;
default_type application/octet-stream;
  
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
  
#access_log logs/access.log main;
  
sendfile on;
# tcp_nopush on;
  
keepalive_timeout 65;
  
# gzip壓縮功能設置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json 
application/javascript application/x-javascript application/xml;
gzip_vary on;
  
# http_proxy 設置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
  
# 設定負載均衡后台服務器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
  
# 很重要的虛擬主機配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
  
charset utf-8;
access_log logs/host.access.log main;
  
#對 / 所有做負載均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
  
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  
}
  
#靜態文件,nginx自己處理,不去backend請求tomcat
location ~* /download/ {
root /apps/oa/fs;
  
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
  
location ~ ^/(WEB-INF)/ {
deny all;
}
#error_page 404 /404.html;
  
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
  
## 其它虛擬主機,server 指令開始
}

  

## 在虛擬主機 server {} 中加入上面配置,也可以單獨定義一個專門用於監控的虛擬主機。
## deny all , 拒絕除 allow 中的主機之外所有主機訪問此 URL ,實現過程中如果遇到 403 ,有可能是你把自己測試的機器拒絕了!

此處默認只有本地訪問,如果遠程可以查看需要加相關的IP或者干脆去掉Deny all即可。加密文件可以使用#htpasswd -c /usr/nginx/conf hxb 命令來創建。配置完成后需要重啟Nginx服務。

Nginx 監控項解釋:

[root@localhost local]# curl http://127.0.0.1/nginx_status
Active connections: 1 
server accepts handled requests
 22 22 72 
Reading: 0 Writing: 1 Waiting: 0 

  

瀏覽器訪問:

 

輸出含義:
Active connections    //當前 Nginx 正處理的活動連接數。
server accepts handledrequests //總共處理了20個連接 , 成功創建 20 次握手,總共處理了63個請求。
Reading //nginx 讀取到客戶端的 Header 信息數。
Writing //nginx 返回給客戶端的 Header 信息數。
Waiting //開啟 keep-alive 的情況下,這個值等於 active – (reading + writing),意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連接

2.通過命令查看:

#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

  

補充:

 查看Nginx並發進程數:ps -ef | grep nginx | wc -l

 查看Web服務器TCP連接狀態:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM