1. 程式人生 > >【轉】Nginx work_processes配置

【轉】Nginx work_processes配置

worker_processes:作業系統啟動多少個工作程序執行Nginx。注意是工作程序,不是有多少個nginx工程。在Nginx執行的時候,會啟動兩種程序,一種是主程序master process;一種是工作程序worker process。例如我在配置檔案中將worker_processes設定為4,啟動Nginx後,使用程序檢視命令觀察名字叫做nginx的程序資訊,我會看到如下結果:

[[email protected] 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       2312   2299  0  80   0 - 28166 -      22:24 pts/0    00:00:00 grep --color=auto nginx
  •  

圖中可以看到1個nginx主程序,master process;還有四個工作程序,worker process。主程序負責監控埠,協調工作程序的工作狀態,分配工作任務,工作程序負責進行任務處理。一般這個引數要和作業系統的CPU核心數成倍數。

worker_connections:這個屬性是指單個工作程序可以允許同時建立外部連線的數量。無論這個連線是外部主動建立的,還是內部建立的。這裡需要注意的是,一個工作程序建立一個連線後,程序將開啟一個檔案副本。所以這個數量還受作業系統設定的,程序最大可開啟的檔案數有關

 

 

設定Nginx程序最大可開啟檔案數

 

1、更改作業系統級別的“程序最大可開啟檔案數”的設定

Linux問題—設定“程序最大可開啟的檔案數”永久有效的方式。

2、更改Nginx軟體級別的“程序最大可開啟檔案數”的設定

剛才更改的只是作業系統級別的“程序最大可開啟檔案”的限制,作為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

3、驗證Nginx的“程序最大可開啟檔案數”是否起作用

那麼我們如何來驗證配置是否起作用了呢?在linux系統中,所有的程序都會有一個臨時的核心配置檔案描述,存放路徑在 /pro/程序號/limit

首先我們來看一下,沒有進行引數優化前的程序配置資訊:

[[email protected] 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。我們選擇一個程序,檢視其核心配置資訊:

[[email protected] 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後,配置資訊就是下圖所示了:

[[email protected] 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