码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx高并发设置跟压测

时间:2018-05-24 20:39:02      阅读:200      评论:0      收藏:0      [点我收藏+]

标签: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高并发设置跟压测

标签:Nginx高并发设置跟压测   nginx压测   nginx高并发设置   nginx防cc设置   

原文地址:http://blog.51cto.com/8999a/2120009

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!