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

相關文章
相關標籤/搜索