經過查看Nginx的併發鏈接,咱們能夠更清除的知道網站的負載狀況。Nginx併發查看有兩種方法(之因此這麼說,是由於筆者只知道兩種),一種是經過web界面,一種是經過命令,web查看要比命令查看顯示的結果精確一些。下面介紹這兩種查看方法;javascript
1.經過瀏覽器查看php
經過web界面查看時Nginx須要開啓status模塊,也就是安裝Nginx時加上 –with-http_stub_status_module 而後配置Nginx.conf,在server點裏面加入以下內容。css
獲取 Nginx 狀態( http_stub _status )html
[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
## 查看編譯時有沒有加入狀態監控模塊,若是沒有須要單獨加載java
1)編譯添加http_stub _statusnginx
[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.confweb
[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配置;json
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]}'