溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx服務(wù)優(yōu)化(六)更改進程數(shù)

發(fā)布時間:2020-07-06 15:32:08 來源:網(wǎng)絡(luò) 閱讀:738 作者:wx5d2c2d660c282 欄目:系統(tǒng)運維

更改Nginx運行進程數(shù)

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

更改進程數(shù)的配置方法

修改配置文件的worker_processes參數(shù)

  • 一般設(shè)為CPU的個數(shù)或者核數(shù)
  • 在高并發(fā)情況下可設(shè)置為CPU個數(shù)或者核數(shù)的2倍

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

Worker_cpu_affinity 0001 0010 0100 1000

1.將虛擬機配置由1核改為2核(關(guān)閉虛擬機)

Nginx服務(wù)優(yōu)化(六)更改進程數(shù)

2.查看配置文件當(dāng)前的進程數(shù)

[root@localhost nginx]# vim conf/nginx.conf
worker_processes  1;   //進程數(shù)1
events {
    worker_connections  1024;  //一個進程處理的請求數(shù)
}
[root@localhost nginx]# ps aux | grep "nginx"   //查看進程數(shù)
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
........//省略部分內(nèi)容

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
........//省略部分內(nèi)容

4.修改配置文件的進程數(shù)

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

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

events {
    worker_connections  1024;
}

[root@localhost ~]# service nginx start    //開啟服務(wù)
[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 ~]# 
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI