Nginx 性能优化之net.core.somaxconn

Ref:http://wiki.51osos.com/wiki/Nginx_%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E4%B9%8Bnet.core.somaxconn

web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值。如:

net.core.netdev_max_backlog = 262144

我们线上服务器net.core.somaxconn都是默认的128,这个参数会影响到所有AF_INET类型socket的listen队列

Man 2 listen可以知道:

int listen(int s, int backlog);

The  backlog  parameter  defines the maximum length the queue of pending connections may grow to.  If a connection request

       arrives with the queue full the client may receive an error with an indication of ECONNREFUSED or, if the underlying  pro-

       tocol supports retransmission, the request may be ignored so that retries succeed.

BUGS

       If the socket is of type AF_INET, and the backlog argument is greater than the constant SOMAXCONN  (128  in  Linux  2.0  &

       2.2),  it  is silently truncated to SOMAXCONN.

也就是说,web应用中listen函数的backlog会给我们内核参数的net.core.somaxconn 限制到128,在高突发的请求中可能会导致链接超时或者触发重传


比如nginx 定义NGX_LISTEN_BACKLOG默认到511, 却由于我们参数未曾优化会限制到128,只有128个connections can be queued in kernel listen queue(by Igor Sysoev).

  1. define NGX_LISTEN_BACKLOG 511/ls.backlog = NGX_LISTEN_BACKLOG;/ if (listen(s, ls.backlog) == -1) {

相信其他应用比如squid也会有类似问题,突发的大并发connect请求会由于内核listen队列的限制导致链接超时或者重传,从而影响用户体验

以下是实验测试情况,使用2台机器分别以1000个并发,benchmark方式,请求服务器 ( 相当于2000个并发请求同时请求服务器 )

情景1,默认配置, net.core.somaxconn=128,服务器为nginx

测试客户端A:

Transactions:                2072870 hits

Availability:                  99.99 %

Elapsed time:                 179.59 secs

Data transferred:            6096.59 MB

Response time:                  0.08 secs

Transaction rate:           11542.24 trans/sec

Throughput:                    33.95 MB/sec

Concurrency:                  927.34

Successful transactions:     2072871

Failed transactions:             300

Longest transaction:           45.30

Shortest transaction:           0.00

错误率大概是1.5%


测试客户端B:

Transactions:                1859454 hits

Availability:                  99.99 %

Elapsed time:                 179.11 secs

Data transferred:            5468.90 MB

Response time:                  0.09 secs

Transaction rate:           10381.63 trans/sec

Throughput:                    30.53 MB/sec

Concurrency:                  904.45

Successful transactions:     1859454

Failed transactions:             276

Longest transaction:           49.60

Shortest transaction:           0.00

错误率大概也是1.5%

错误提示大都为:

socket: connection timed out

warning: socket: -1803417280 select timed out: Connection timed out


情景2,调整配置, net.core.somaxconn=8192, nginx显式配置 listen 80 default backlog=8192;

测试客户端A:

** SIEGE 2.69

** Preparing 1000 concurrent users for battle.

The server is now under siege...

Lifting the server siege...      done.

Transactions:                1789818 hits

Availability:                 100.00 %

Elapsed time:                 180.00 secs

Data transferred:            5264.09 MB

Response time:                  0.10 secs

Transaction rate:            9943.43 trans/sec

Throughput:                    29.24 MB/sec

Concurrency:                  997.06

Successful transactions:     1789818

Failed transactions:               0

Longest transaction:            0.87

Shortest transaction:           0.00

错误率是0


测试客户端B:

** SIEGE 2.69

** Preparing 1000 concurrent users for battle.

The server is now under siege...

Lifting the server siege...      done.

Transactions:                1768585 hits

Availability:                 100.00 %

Elapsed time:                 179.31 secs

Data transferred:            5201.65 MB

Response time:                  0.10 secs

Transaction rate:            9863.28 trans/sec

Throughput:                    29.01 MB/sec

Concurrency:                  998.30

Successful transactions:     1768588

Failed transactions:               0

Longest transaction:            3.10

Shortest transaction:           0.03

错误率是0 nginx的配置中增加监听队列的数量,当然前提是不能超过net.core.somaxconn的值。如:

server {
        listen 80 backlog=8192;
        server_name http://www.linuxgit.org;
    }


转载于:https://my.oschina.net/hongsheng/blog/151136

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx的配置文件主要包括nginx.conf和conf.d目录下的配置文件。 nginx.conf是nginx的主配置文件,其中包含了全局的配置指令,如工作进程数量、日志文件路径、服务器块等。在nginx.conf中可以使用access_log指令来配置访问日志的路径和格式。例如,access_log /usr/local/nginx/logs/host.access.log main; 可以将访问日志记录在/usr/local/nginx/logs/host.access.log文件中。 而conf.d目录则是用来存放nginx的虚拟主机或者其他模块的配置文件的目录。在conf.d目录下的配置文件会被nginx主配置文件引入。例如,access_log /usr/local/nginx/logs/host.access.404.log log404; 可以将404错误的访问日志记录在/usr/local/nginx/logs/host.access.404.log文件中。 对于指令中提到的ulimit -n的值,它是指一个nginx进程打开的最多文件描述符数目。根据最佳实践,nginx的worker_connections指令应该与ulimit -n的值保持一致,以确保nginx能够处理最大数量的并发连接请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Nginx配置文件 nginx.conf 和default.conf 讲解](https://blog.csdn.net/rdhj5566/article/details/121329006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值