【Nginx】Nginx开启线程池(Thread Pool)以提升性能

        众所周知,Nginx一款体积小巧,但是性能强大的软负载,主要被用作后端服务和应用的反向代理和负载均衡。

        Nginx的编译可以配置很多参数,但是默认情况下是不会开启线程池(社区版从1.7.11开始引入线程池)的。因此无论是master进程,还是worker进程的线程数都是1,如下所示:

# ps -ef| grep nginx
root      8304  8232  0 09:33 pts/1    00:00:00 grep --color=auto nginx
root     16365 21060  1 Oct28 ?        02:04:32 nginx: worker process
root     16366 21060  2 Oct28 ?        02:48:07 nginx: worker process
root     16367 21060  2 Oct28 ?        02:21:11 nginx: worker process
root     16368 21060  3 Oct28 ?        03:55:30 nginx: worker process
root     21060     1  0 Jul31 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx    

# ps -efL| grep 21060| grep -v grep| wc -l
5
# ps -efL| grep 16365| grep -v grep| wc -l
1
# ps -efL| grep 16366| grep -v grep| wc -l
1
# ps -efL| grep 16367| grep -v grep| wc -l
1
# ps -efL| grep 16368| grep -v grep| wc -l
1

        现在我们在编译的时候,增加--with-threads参数,然后在nginx.conf中配置aio threads后,再来看看master进程和worker进程的默认线程数,默认的线程池的线程数为32个。

# ./configure --prefix=/data/nginx --with-threads
# make
# make install
# ps -ef| grep nginx| grep -v grep
root       4155      1  0 09:10 ?        00:00:00 nginx: master process ../sbin/nginx
root       4156   4155  0 09:10 ?        00:00:00 nginx: worker process
root       4157   4155  0 09:10 ?        00:00:00 nginx: worker process
root       4158   4155  0 09:10 ?        00:00:00 nginx: worker process
root       4159   4155  0 09:10 ?        00:00:00 nginx: worker process

# ps -efL| grep 4156| grep -v grep| wc -l
33
# ps -efL| grep 4157| grep -v grep| wc -l
33
# ps -efL| grep 4158| grep -v grep| wc -l
33
# ps -efL| grep 4159| grep -v grep| wc -l
33
# ps -efL| grep 4155| grep -v grep| wc -l
133

        现在,我们再来研究下自定义线程池(一个CPU内核最大不要超过50个线程,我的测试虚拟机是4核),尝试修改线程池的线程数为120(一个核配置30个线程)试试。

----nginx.conf配置片段
----它定义了一个名为default的线程池,其中包含 120 个工作线程和 1024 个任务的任务队列的最大长度。如果任务队列过载,NGINX 会拒绝请求并记录此错误

# in the 'main' context
thread_pool default threads=120 max_queue=1024;
 
# in the 'http', 'server', or 'location' context
aio threads=default;

# ps -ef| grep nginx| grep -v grep
root       4155      1  0 09:10 ?        00:00:00 nginx: master process ../sbin/nginx
root       8711   4155  2 09:47 ?        00:00:00 nginx: worker process
root       8712   4155  2 09:47 ?        00:00:00 nginx: worker process
root       8713   4155  2 09:47 ?        00:00:00 nginx: worker process
root       8714   4155  3 09:47 ?        00:00:00 nginx: worker process

# ps -efL| grep 4155| grep -v grep| wc -l
485
# ps -efL| grep 8711| grep -v grep| wc -l
121
# ps -efL| grep 8712| grep -v grep| wc -l
121
# ps -efL| grep 8713| grep -v grep| wc -l
121
# ps -efL| grep 8714| grep -v grep| wc -l
121

使用apache jmeter进行性能压测,线程组配置:

HTTP请求配置:

 吞吐量压测数据(5次):

 

参考:

https://www.nginx.com/blog/thread-pools-boost-performance-9x/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cnskylee

技术分享我是认真的,期待您打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值