worker_processes:操做系统启动多少个工做进程运行Nginx。注意是工做进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工做进程worker process。例如我在配置文件中将worker_processes设置为4,启动Nginx后,使用进程查看命令观察名字叫作nginx的进程信息,我会看到以下结果:linux
[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 2312 2299 0 80 0 - 28166 - 22:24 pts/0 00:00:00 grep --color=auto nginx
图中能够看到1个nginx主进程,master process;还有四个工做进程,worker process。主进程负责监控端口,协调工做进程的工做状态,分配工做任务,工做进程负责进行任务处理。通常这个参数要和操做系统的CPU内核数成倍数。nginx
worker_connections:这个属性是指单个工做进程能够容许同时创建外部链接的数量。不管这个链接是外部主动创建的,仍是内部创建的。这里须要注意的是,一个工做进程创建一个链接后,进程将打开一个文件副本。因此这个数量还受操做系统设定的,进程最大可打开的文件数有关。web
Linux问题—设置“进程最大可打开的文件数”永久有效的方式。bash
刚才更改的只是操做系统级别的“进程最大可打开文件”的限制,做为Nginx来讲,咱们还要对这个软件进行更改。打开nginx.conf主配置文件。您须要配合worker_rlimit_nofile属性。以下:ide
user root root;
worker_processes 4;
worker_rlimit_nofile 65535;svg#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;
}spa
这里只粘贴了部分代码,其余的配置代码和主题无关,也就不须要粘贴了。请注意代码行中加粗的两个配置项,请必定两个属性所有配置。配置完成后,请经过nginx -s reload命令从新启动Nginx。操作系统
那么咱们如何来验证配置是否起做用了呢?在linux系统中,全部的进程都会有一个临时的核心配置文件描述,存放路径在 /pro/进程号/limit。.net
首先咱们来看一下,没有进行参数优化前的进程配置信息:
[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