返回列表 发帖

Nginx的并发连接数

笺注:Nginx的安装可以参考: CentOS6_Nginx反向代理+负载均衡(轮询)


服务器的逻辑CPU个数:(CPU核心数)
lscpu
图片1.png
2021-7-11 12:25



查看Nginx的主配置文件:
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf |grep -v "^$"
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
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  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #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;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}


修改Nginx的主配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

worker_processes auto;  #Nginx开启的worker进程数;可以设置为自动(auto),这样Nginx会自动根据逻辑CPU个数生成对应数量的worker进程

worker_rlimit_nofile 65535;  #Nginx的worker进程的最大可打开文件数

events {
        use epoll;
        worker_connections 51200;  #单个worker进程的最大并发连接数;要根据服务器性能(内存使用量等等)来调整
        multi_accept on;  #开启时,一个worker进程可以同时接受所有的新连接,建议开启
        accept_mutex off;  #缺省时开启,避免了惊群问题;关闭时,新连接会通报给所有worker进程,网站访问量比较大时,建议关闭
}

如下图:(截图有省略)
图片2.png
2021-7-11 12:27


笺注:
查看服务器的逻辑CPU个数:(CPU核心数)
[root@localhost ~]# cat /proc/cpuinfo | grep processor | wc -l
1


Nginx作为Web服务器的时候:
Nginx的最大并发连接数 = worker_processes * worker_connections / 2

Nginx作为反向代理服务器的时候:
Nginx的最大并发连接数 = worker_processes * worker_connections / 4


重启Nginx服务:
[root@localhost ~]# service nginx restart


测试Nginx的进程最大可打开文件数,找到Nginx的worker进程的ID:
ps -ef |grep nginx |grep -v grep
图片3.png
2021-7-11 12:28


cat /proc/4675/limits
图片4.png
2021-7-11 12:28



使用命令ab进行测试:

[root@localhost ~]# yum -y install httpd-tools

[root@localhost ~]# ab -n 10000 -c 1000 http://127.0.0.1/index.html #指定并发请求数为1000,总请求数为10000,对http://127.0.0.1/index.html进行压力测试
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 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.10.0
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      1000
Time taken for tests:   1.109 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      8662095 bytes
HTML transferred:       6273612 bytes
Requests per second:    9013.97 [#/sec] (mean)
Time per request:       110.939 [ms] (mean)
Time per request:       0.111 [ms] (mean, across all concurrent requests)
Transfer rate:          7624.99 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   31  14.0     28      76
Processing:    17   32  10.3     29      77
Waiting:        0   26   9.2     24      75
Total:         41   64  23.5     57     151

Percentage of the requests served within a certain time (ms)
  50%     57
  66%     58
  75%     59
  80%     60
  90%    123
  95%    131
  98%    141
  99%    143
100%    151 (longest request)

注释:可以看到每秒最大处理请求次数为9013.97次,每个请求响应时间是0.111毫秒。



笺注:
Nginx的最大并发连接数受限于Max open files,当超过限制值时,会出现Too many open files的信息:
#指定并发请求数为2500,总请求数为10000时:
[root@localhost ~]# ab -n 10000 -c 2500 http://127.0.0.1/index.html
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 127.0.0.1 (be patient)
socket: Too many open files (24)



而Max open files受限于操作系统的“进程最大可打开文件数”,操作系统的“进程最大可打开文件数”的查询方法:
[root@localhost ~]# ulimit -n
1024


操作系统的“进程最大可打开文件数”的修改方法:
[root@localhost ~]# tail -5 /etc/security/limits.conf
# End of file
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535



重启服务器:
[root@localhost ~]# reboot


服务器重启后,更改生效:
[root@localhost ~]# ulimit -n
65535



再次测试Nginx的进程最大可打开文件数:(服务器重启后,要重启Nginx服务才生效)
[root@localhost ~]# ps -ef |grep nginx |grep -v grep
root       1550      1  0 05:14 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      1551   1550  0 05:14 ?        00:00:00 nginx: worker process  

cat /proc/1550/limits
图片5.png
2021-7-11 12:31




再次测试,指定并发请求数为2500,总请求数为10000时:
[root@localhost ~]# ab -n 10000 -c 2500 http://127.0.0.1/index.html
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 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.10.0
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      2500
Time taken for tests:   1.089 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      8676460 bytes
HTML transferred:       6284016 bytes
Requests per second:    9186.81 [#/sec] (mean)
Time per request:       272.129 [ms] (mean)
Time per request:       0.109 [ms] (mean, across all concurrent requests)
Transfer rate:          7784.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   36  18.5     27      90
Processing:    19   43  30.3     28     232
Waiting:        1   35  27.9     22     230
Total:         44   80  46.3     53     287

Percentage of the requests served within a certain time (ms)
  50%     53
  66%     55
  75%    140
  80%    144
  90%    163
  95%    173
  98%    179
  99%    180
100%    287 (longest request)

注释:可以看到每秒最大处理请求次数为9186.81次,每个请求响应时间是0.109毫秒。





######

检测Nginx的进程最大可打开文件数的脚本:(假如Max open files的限制值不是65535,就重启Nginx服务)

[root@localhost ~]# cat check_Nginx_Max_open_files.sh
#!/bin/bash

a=`ps -ef |grep nginx |grep -v grep |grep 'worker process' |awk '{print $3}'`

b=`cat /proc/$a/limits |grep 'open' |awk '{print $4'}`

if [ $b -ne 65535 ]; then
/sbin/service nginx restart
fi


结合任务计划:
[root@localhost ~]# crontab -e
追加:
*/30 * * * * bash /root/check_Nginx_Max_open_files.sh





######

查看当前Nginx的并发连接数:

修改Nginx的主配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
插入以下代码:
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

如下图:
图片6.png
2021-7-11 12:34



重启Nginx服务:
[root@localhost ~]# service nginx restart



服务器本地测试:
[root@localhost ~]# curl http://127.0.0.1/nginx_status
Active connections: 7  #Nginx正在处理的活动连接数有多少个(当前Nginx的并发连接数)
server accepts handled requests
1319 1319 1055
Reading: 0 Writing: 1 Waiting: 6





相关文章:
CentOS8_lnmp1.7_单独安装Nginx

返回列表