找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 233|回复: 1

[L/WNAMP] Nginx开启线程池(Thread Pool)以提升性能

  [复制链接]
发表于 2023-2-24 16:50 | 显示全部楼层 |阅读模式

众所周知,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/

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
 楼主| 发表于 2023-2-24 17:02 | 显示全部楼层
知道了原理,对于已经安装好的nginx难道再安装一次?如果是支持线程池的版本,当然不用。只需要配置一下参数即可。

Nginx线程池配置

使用线程池功能,首先需要在配置文件中添加如下配置项:

location / {
    root /html;
    thread_pool default threads=32 max_queue=65535;
    aio threads=default;
}

上面定义了一个名为“default”,包含32个线程,任务队列最多支持65535个请求的线程池。如果任务队列过载,Nginx将输出如下错误日志并拒绝请求:

thread pool "default" queue overflow: N tasks waiting

如果出现上面的错误,说明线程池的负载很高,这是可以通过添加线程数来解决这个问题。当达到机器的最高处理能力之后,增加线程数并不能改善这个问题 。

可在编译时使用如下选项可以启用线程池功能

  1. --with-threads

  2. --with-file-aio

启用线程池功能,让请求排队等待处理,并且可以充分利用 CPU 提高处理效率,开启线程池需要 AIO 的支持,启用异步文件 IO (AIO) 一般用于大文件传输的场景;

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|学习笔记

GMT+8, 2024-5-5 12:08 , Processed in 0.029849 second(s), 13 queries , APCu On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表