【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进行性能压测,线程组配置:

【Nginx】Nginx开启线程池(Thread Pool)以提升性能_第1张图片

HTTP请求配置:

【Nginx】Nginx开启线程池(Thread Pool)以提升性能_第2张图片 吞吐量压测数据(5次):

 

参考:

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

你可能感兴趣的:(Nginx,nginx,with-threads,thread_pool)