[Nginx入门到实战]知识点补充-Nginx的优化参数worker_connections

我的实战课程(Nginx的服务优化篇中),同学得提出的问题,如下:
图片描述

关于Nginx的worker_connections到底是什么?如果设置过小会有什么影响,下面我用一个场景演示下:

  1. 首先,我的场景如下:

图片描述

  1. 然后,说下Nginx下的配置

1、nginx.conf

worker_processes  1;
events {
    worker_connections  6;
}

2、default.conf

    location / {
        proxy_pass http://www.youku.com/;
        index index.html index.htm;
    }

    location /status {
        stub_status on;
    }

3 最后,用ab 工具进行压测:

场景1、将worker_connections 2,ab -c 1

curl的结果如下:

$ curl -I  http://116.62.103.228/
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.1
Date: Tue, 15 Aug 2017 13:58:05 GMT
Content-Type: text/html
Content-Length: 537
Connection: close
ETag: "5964d79b-219

场景2、将worker_connections 4,ab -c 1
curl 正常,返回200;
然后,将worker_connections 6,ab -c 2
可以支持到并发两个连接。

场景1、2 结论:反向代理的模式下,客户端n个请求,服务端需要建立n2+2个连接。发现需要多余出两个连接
所以 最大的用户连接数 = (worker_connections worker_processes - 2)/2

场景3、将worker_connections 12,ab -n 20 -c 3 http://116.62.103.228/
访问我的连接状态地址:http://116.62.103.228/status
Active connections: 4
用netstat查看:
图片描述
发现除开本身访问/status连接,正好连接数为:3个

场景3得出结论:
worker_connections 限制指的是单个worker对并发的连接数。

场景4 加大并发数目 ab -c 9如下:
ab -n 9 -c 9 http://116.62.103.228/
出现这个错误:apr_socket_recv: Connection reset by peer
另外,再查看nginx的error log出现如下:

2017/08/15 23:13:36 [alert] 15740#15740: 12 worker_connections are not enough
2017/08/15 23:13:36 [alert] 15740#15740: 12 worker_connections are not enough
2017/08/15 23:13:36 [alert] 15740#15740: 12 worker_connections are not enough
2017/08/15 23:13:36 [alert] 15740#15740: 12 worker_connections are not enou


作者:Jeson
链接:https://www.imooc.com/article/19907
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作

原文地址:https://www.cnblogs.com/gao88/p/12103082.html