Nginx服務優化(六)更改進程數

更改Nginx運行進程數

在高併發場景,需要啓動更多的Nginx進程以保證快速響應,以處理用戶的請求,避免造成阻塞。運行進程數多一些,響應訪問請求時,Nginx就不會臨時啓動新的進程提供服務,減少了系統的開銷,提升了服務速度,使用ps aux可以查看運行進程數的變化情況。

更改進程數的配置方法

修改配置文件的worker_processes參數

  • 一般設爲CPU的個數或者核數
  • 在高併發情況下可設置爲CPU個數或者核數的2倍

默認情況,Nginx的多個進程可能跑在一個CPU上, 可以分配不同的進程給不同的CPU處理,充分利用硬件多
核多CPU在一臺4核物理服務器可進行以下配置,將進程進行分配

Worker_cpu_affinity 0001 0010 0100 1000

1.將虛擬機配置由1核改爲2核(關閉虛擬機)

2.查看配置文件當前的進程數

[root@localhost nginx]# vim conf/nginx.conf
worker_processes  1;   //進程數1
events {
    worker_connections  1024;  //一個進程處理的請求數
}
[root@localhost nginx]# ps aux | grep "nginx"   //查看進程數
root      61991  0.0  0.0  20548   616 ?        Ss   19:08   0:00 nginx: master process /usr/local/nginx/sbin/nginx
//主進程
nginx     61995  0.0  0.0  23076  1644 ?        S    19:08   0:00 nginx: worker process  //工作進程爲1
root      62145  0.0  0.0 112728   968 pts/0    R+   19:16   0:00 grep --color=auto nginx   //ps命令的進程
[root@localhost nginx]# 

3.查看cpu信息

[root@localhost ~]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 158
model name	: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
stepping	: 10
microcode	: 0xaa
cpu MHz		: 2591.568
........//省略部分內容

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 158
model name	: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
stepping	: 10
microcode	: 0xaa
cpu MHz		: 2591.568
........//省略部分內容

4.修改配置文件的進程數

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  2;   //進程數該爲2
worker_cpu_affinity 01 10;   //進程平均分配到兩個CPU上,01、10爲二進制編號

events {
    worker_connections  1024;
}

[root@localhost ~]# service nginx start    //開啓服務
[root@localhost ~]# ps aux | grep "nginx"
root       2593  0.0  0.0  20548   612 ?        Ss   13:57   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      2594  0.0  0.0  23076  1392 ?        S    13:57   0:00 nginx: worker process   //進程1
nginx      2595  0.0  0.0  23076  1376 ?        S    13:57   0:00 nginx: worker process   //進程2
root       2603  0.0  0.0 112728   968 pts/0    S+   13:57   0:00 grep --color=auto nginx
[root@localhost ~]# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章