如下是一个完整的nginx配置信息。html
依靠event loop机制,单个线程能够同时处理多个request请求,并在处理完产生response的时候,回调相关的远程事件。根据NIO实现机制的不一样,技术名称也就不一样了。linux
epoll、kqueue 等这些组件都是对多路复用网络I/O模型的实现,其中epoll是poll的升级版本,在linux环境下可使用但限于linux2.6及以上版本。kqueue用在bsd上使用。nginx
worker_processes:操做系统启动多少个工做进程运行Nginx。注意是工做进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工做进程worker process。例如我在配置文件中将worker_processes设置为4,启动Nginx后,使用进程查看命令观察名字叫作nginx的进程信息,我会看到以下结果:正则表达式
图中能够看到1个nginx主进程,master process;还有四个工做进程,worker process。主进程负责监控端口,协调工做进程的工做状态,分配工做任务,工做进程负责进行任务处理。通常这个参数要和操做系统的CPU内核数成倍数。算法
worker_connections:这个属性是指单个工做进程能够容许同时创建外部链接的数量。不管这个链接是外部主动创建的,仍是内部创建的。这里须要注意的是,一个工做进程创建一个链接后,进程将打开一个文件副本。因此这个数量还受操做系统设定的,进程最大可打开的文件数有关。shell
更改操做系统级别的“进程最大可打开文件数”的设置:vim
修改limits.conf主配置文件后端
vim /etc/security/limits.conf服务器
在主配置文件最后加入下面两句:网络
* soft nofile 65535
* hard nofile 65535
这两句话的含义是soft(应用软件)级别限制的最大可打开文件数的限制,hard表示操做系统级别限制的最大可打开文件数的限制。
保存这个文件后,配置是不会立刻生效的,为了保证本次shell会话中的配置立刻有效,咱们须要经过ulimit命令更改本次的shell会话设置。
ulimit -n 65535
执行命令后,配置立刻生效。您能够用ulimit -a 查看目前会话中的全部核心配置:
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7746
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 7746
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
请注意open files这一项。
更改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。
验证Nginx的“进程最大可打开文件数”是否起做用:
在linux系统中,全部的进程都会有一个临时的核心配置文件描述,存放路径在/pro/进程号/limit。
首先咱们来看一下,没有进行参数优化前的进程配置信息:
ps -elf | grep nginx
1 S root 1594 1 0 80 0 - 6070 rt_sig 05:06 ? 00:00:00 nginx: master process /usr/nginx-1.8.0/sbin/nginx
5 S root 1596 1594 0 80 0 - 6176 ep_pol 05:06 ? 00:00:00 nginx: worker process
5 S root 1597 1594 0 80 0 - 6176 ep_pol 05:06 ? 00:00:00 nginx: worker process
5 S root 1598 1594 0 80 0 - 6176 ep_pol 05:06 ? 00:00:00 nginx: worker process
5 S root 1599 1594 0 80 0 - 6176 ep_pol 05:06 ? 00:00:00 nginx: worker process
能够看到,nginx工做进程的进程号是:1596 1597 1598 1599。咱们选择一个进程,查看其核心配置信息:
cat /proc/1598/limits
请注意其中的Max open files ,分别是1024和4096。那么更改配置信息,并重启Nginx后,配置信息就是下图所示了: