worker_processes:操作系統(tǒng)啟動多少個工作進程運行Nginx
注意是工作進程,不是有多少個nginx工程。在Nginx運行的時候,會啟動兩種進程,一種是主進程master process;一種是工作進程worker process。
例如我在配置文件中將worker_processes設置為4.
在這里插入圖片描述
圖中可以看到1個nginx主進程,master process;還有四個工作進程,worker process。主進程負責監(jiān)控端口,協(xié)調(diào)工作進程的工作狀態(tài),分配工作任務,工作進程負責進行任務處理。一般這個參數(shù)要和操作系統(tǒng)的CPU內(nèi)核數(shù)成倍數(shù)。
worker_connections:這個屬性是指單個工作進程可以允許同時建立外部連接的數(shù)量
無論這個連接是外部主動建立的,還是內(nèi)部建立的。這里需要注意的是,一個工作進程建立一個連接后,進程將打開一個文件副本。所以這個數(shù)量還受操作系統(tǒng)設定的,進程最大可打開的文件數(shù)有關。
更改操作系統(tǒng)級別的“進程最大可打開文件數(shù)”的設置
Linux設置“進程最大可打開的文件數(shù)”永久有效的方式
更改Nginx軟件級別的“進程最大可打開文件數(shù)”的設置
剛才更改的只是操作系統(tǒng)級別的“進程最大可打開文件”的限制,作為Nginx來說,我們還要對這個軟件進行更改。打開nginx.conf主配置文件。您需要配合worker_rlimit_nofile屬性。如下:
user root root;
worker_processes 4;
worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
請注意代碼行中加粗的兩個配置項,請一定兩個屬性全部配置。配置完成后,請通過nginx -s reload命令重新啟動Nginx。
驗證Nginx的“進程最大可打開文件數(shù)”是否起作用
在linux系統(tǒng)中,所有的進程都會有一個臨時的核心配置文件描述,存放路徑在 /pro/進程號/limit。
[root@localhost nginx]# ps -elf | grep nginx
4 S root 2203 2031 0 80 0 - 46881 wait 22:18 pts/0 00:00:00 su nginx
4 S nginx 2204 2203 0 80 0 - 28877 wait 22:18 pts/0 00:00:00 bash
5 S root 2252 1 0 80 0 - 11390 sigsus 22:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 S nobody 2291 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
5 S nobody 2292 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
5 S nobody 2293 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
5 S nobody 2294 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
0 R root 2318 2299 0 80 0 - 28166 - 22:42 pts/0 00:00:00 grep --color=auto nginx
可以看到,nginx工作進程的進程號是:2291 2292 2293 2294。我們選擇一個進程,查看其核心配置信息:
[root@localhost nginx]# cat /proc/2291/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 3829 3829 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 3829 3829 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
請注意其中的Max open files ,分別是1024和4096。那么更改配置信息,并重啟Nginx后,配置信息就是下圖所示了:
[root@localhost conf]# cat /proc/2351/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 3829 3829 processes
Max open files 65535 65535 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 3829 3829 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
參考了大佬的博文
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。