1. 程式人生 > >Nginx高並發設置跟壓測

Nginx高並發設置跟壓測

Nginx高並發設置跟壓測 nginx壓測 nginx高並發設置 nginx防cc設置

Nginx高並發設置跟壓測

環境:
Centos6.5 nginx1.14.0
安裝
Nginx 壓測工具ab
步驟:
Nginx安裝部署網站步驟略,綁定域名www.langba888.com 192.168.137.49測試
ab: yum install httpd-tools
ab -V 看看顯示版本

Nginx limit模塊限制並發數設置
如何Nginx限制同一個ip的連接數,限制並發數目:
1.添加limit_zone和limit_req_zone 這個變量只能在http使用 :
vim /etc/nginx/nginx.conf
limit_conn_zone $binary_remote_addr zone=one:20m;

limit_req_zone $binary_remote_addr zone=req_one:20m rate=12r/s;
2.添加limit_conn 和limit_req 這個變量可以在http, server, location使用 我是限制nginx上的所有服務,所以添加到http裏面 (如果你需要限制部分服務,可在nginx/conf/domains裏面選擇相應的server或者location添加上便可)
vi /etc/nginx/nginx.conf
limit_conn_zone $binary_remote_addr zone=one:20m;
limit_req_zone $binary_remote_addr zone=req_one:20m rate=12r/s;
limit_conn one 10;
limit_req zone=req_one burst=120;
參數詳解(數值按具體需要和服務器承載能力設置,):
limit_zone,是針對每個變量(這裏指IP,即$binary_remote_addr)定義一個存儲session狀態的容器。這個示例中定義了一個20m的容器,按照32bytes/session,可以處理640000個session。
limit_req_zone 與limit_conn_zone類似。rate是請求頻率. 每秒允許12個請求。
limit_conn one 10 : 表示一個IP能發起10個並發連接數
limit_req: 與limit_req_zone對應。burst表示緩存住的請求數。
範例:
[root@master nginx]# cat /etc/nginx/nginx.conf

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
use epoll;
worker_connections 1024;
accept_mutex off;
}

http {
limit_conn_zone $binary_remote_addr zone=one:20m;
limit_req_zone $binary_remote_addr zone=req_one:20m rate=12r/s;

limit_conn   one 10;
limit_req   zone=req_one burst=120;

include       /etc/nginx/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  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;

#gzip  on;

include /etc/nginx/conf.d/*.conf;

}

最後nginx -t 進行測試ok 就重啟nginx
/etc/init.d/nginx restart

然後用ab進行測試
[root@master nginx]# ab -c 100 -n 1000 http://www.langba888.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking lt.eg132.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: nginx/1.14.0
Server Hostname: www.langba888.com
Server Port: 80

Document Path: /
Document Length: 169 bytes

Concurrency Level: 100
Time taken for tests: 83.256 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 319000 bytes
HTML transferred: 169000 bytes
Requests per second: 12.01 [#/sec] (mean)
Time per request: 8325.609 [ms] (mean)
Time per request: 83.256 [ms] (mean, across all concurrent requests)
Transfer rate: 3.74 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 7.0 0 31
Processing: 31 7909 1482.9 8333 8336
Waiting: 0 7909 1483.0 8333 8336
Total: 32 7911 1476.3 8333 8336

Percentage of the requests served within a certain time (ms)
50% 8333
66% 8334
75% 8334
80% 8334
90% 8334
95% 8335
98% 8335
99% 8335
100% 8336 (longest request)

ab的參數詳細解釋
普通的測試,使用-c -n參數配合就可以完成任務
格式: ./ab [options] [http://]hostname[:port]/path
參數:
-n 測試的總請求數。默認時,僅執行一個請求
-c 一次並發請求個數。默認是一次一個。
對於返回結果的參數說明:
Server Software: ? ? ? ? ? //Web服務器引擎
Server Hostname: ? ? ? ? //服務器地址
Server Port: ? ? ? ? ? ? ? ? //服務器端口

Document Path: ? ? ? ? ? //請求的文件路徑
Document Length: ? ? ? //文件大小

Concurrency Level: ? ? ? //並發次數
Time taken for tests: ? ?//測試所需時間
Complete requests: ? ? ?//成功請求次數
Failed requests: ? ? ? ? ? //失敗請求次數
? ?(Connect: 0, Length: 73, Exceptions: 0)
Write errors: ? ? ? ? ? ? ? //寫入錯誤
Keep-Alive requests: ? ?
Total transferred: ? ? ? ?//測試過程傳輸字節數
HTML transferred: ? ? ? /HTML內容傳輸字節數
Requests per second: ? //每秒請求數 ( 平均 )
Time per request: ? ? ? ?//每次並發請求時間 ( 所有並發 )
Time per request: ? ? ? ?//每一請求時間 ( 並發平均 )
Transfer rate: ? ? ? ? ? ? ?//平均傳輸速率

Nginx高並發設置跟壓測