典型的,提供大量靜態文件訪問的web服務器,緩存服務器(如squid), 均要注意這個問題
網上的教程,大約只是簡單說明了如何設置ulimit和file-max, 但並沒有說清楚這兩者之間的差別,讓人一頭霧水
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調用,設置每個進程的限制。如果得到大量使用完文件句柄的錯誤信息,是應該增加這個值。
也就是說,這項參數是系統級別的。
echo 6553560 > /proc/sys/fs/file-max
或修改 /etc/sysctl.conf, 加入
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 nofile 65535
* hard nofile 65535
修改完之後,重啓即可生效