file-max与ulimit的设置,nginx 500错误的缘由

典型的,提供大量静态文件访问的web服务器,缓存服务器(如squid), 均要注意这个问题 web


网上的教程,大约只是简单说明了如何设置ulimit和file-max, 但并无说清楚这二者之间的差异,让人一头雾水 shell


1. file-max的含义 缓存

man proc,可获得file-max的描述: 服务器

/proc/sys/fs/file-max
              This  file defines a system-wide limit on the number of open files for all processes.  (See
              also setrlimit(2),  which  can  be  used  by  a  process  to  set  the  per-process  limit,
              RLIMIT_NOFILE,  on  the  number  of  files it may open.)  If you get lots of error messages
              about running out of file handles, try increasing this value:

即file-max是设置
 系统全部进程一共能够打开的文件数量 。同时一些程序能够经过setrlimit调用,设置每一个进程的限制。若是获得大量使用完文件句柄的错误信息,是应该增长这个值。 ide


也就是说,这项参数是系统级别的。 ui


echo  6553560 > /proc/sys/fs/file-max this

或修改 /etc/sysctl.conf, 加入 spa

fs.file-max = 6553560 重启生效 教程


2. ulimit的 进程

Provides control over the resources available to the shell and to processes started by it, on systems that allow  such control.


即设置当前shell以及由它启动的进程的资源限制。


显然,对服务器来讲,file-max, ulimit都须要设置,不然就可能出现文件描述符用尽的问题,为了让机器在重启以后仍然有效,强烈创建做如下配置,以确保file-max, ulimit的值正确无误:


1. 修改/etc/sysctl.conf, 加入

fs.file-max = 6553560


2.系统默认的ulimit对文件打开数量的限制是1024,修改/etc/security/limits.conf并加入如下配置,永久生效

* soft nproc 65535

* hard nproc 65535

* soft nofile 65535 
* hard nofile 65535


修改完以后,重启便可生效



soft nproc: 可打开的文件描述符的最大数(软限制)

hard nproc: 可打开的文件描述符的最大数(硬限制)

soft nofile:单个用户可用的最大进程数量(软限制)

hard nofile:单个用户可用的最大进程数量(硬限制)

限制可使用: ulimit -SHn 65536

相关文章
相关标签/搜索