Ubuntu 12 ulimit 系统最大打开文件个数设置(Nginx中提示Too Many Open Files 错误解决办法)
Too Many Open Files错误提示就是打开的文件太多了,解决办法也很简单我们只要在nginx配置文件中或linux中直接把ulimit 参考设置大一点就可以解决些问题了,当然最主要的还是我们程序上的优化了。
基本命令了解:
root@ubuntu:~# 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) 31498
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 31498
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
红字部分就是打开文件数1024个,一般这个太小了。也可以用ulimit -n查看
如何设置呢,官方是这样的:
第一步:配置/etc/security/limits.conf
sudo vim /etc/security/limits.conf
文件尾追加
* hard nofile 40960
* soft nofile 40960
40960可以自己设置,四列参数的设置见英文,简单讲一下:
第一列,可以是用户,也可以是组,要用@group这样的语法,也可以是通配符如*%
第二列,两个值:hard,硬限制,soft,软件限制,一般来说soft要比hard小,hard是底线,决对不能超过,超过soft报警,直到hard数
第三列,见列表,打开文件数是nofile
第四列,数量,这个也不能设置太大
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
#
#Where:
#<domain> can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for default entry
# - the wildcard %, can be also used with %group syntax,
# for maxlogin limit
# - NOTE: group and wildcard limits are not applied to root.
# To apply a limit to the root user, <domain> must be
# the literal username root.
#
#<type> can have the two values:
# - "soft" for enforcing the soft limits
# - "hard" for enforcing hard limits
#
#<item> can be one of the following:
# - core - limits the core file size (KB)
# - data - max data size (KB)
# - fsize - maximum filesize (KB)
# - memlock - max locked-in-memory address space (KB)
# - nofile - max number of open files
# - rss - max resident set size (KB)
# - stack - max stack size (KB)
# - cpu - max CPU time (MIN)
# - nproc - max number of processes
# - as - address space limit (KB)
# - maxlogins - max number of logins for this user
# - maxsyslogins - max number of logins on the system
# - priority - the priority to run user process with
# - locks - max number of file locks the user can hold
# - sigpending - max number of pending signals
# - msgqueue - max memory used by POSIX message queues (bytes)
# - nice - max nice priority allowed to raise to values: [-20, 19]
# - rtprio - max realtime priority
# - chroot - change root to directory (Debian-specific)
#
#<domain> <type> <item> <value>
#
#* soft core 0
#root hard core 100000
第二步:/etc/pam.d/su(官方)或/etc/pam.d/common-session(网络)
sudo vim /etc/pam.d/su
将 pam_limits.so 这一行注释去掉
重起系统
sudo vim /etc/pam.d/common-session
加上以下一行
session required pam_limits.so
打开/etc/pam.d/su,发现是包含/etc/pam.d/common-session这个文件的,所以修改哪个文件都应该是可以的
这个觉得修改su这个文件比较好,取消注释就OK了,不容易出错,vim打开,定位,x一下即可
官方只到第二步,就重启系统了,没有第三步,好象不行,感觉是不是全是第三步的作用?!
第三步:配置/etc/profile
最后一行加上
ulimit -SHn 40960
重启,ulimit -n 验证,显示40960就没问题了
更新:2012/9/29:
CentOS 6.3上,只要修改/etc/security/limits.conf,重新登录就OK了。
Ubuntu Server 12.04.1上,修改/etc/security/limits.conf,重登录重启不管用,改/etc/pam.d/common-session, su,重登录重启不管用,非要改/etc/profile?
看来两者是有差异的?还是我测试不到位?
nginx.conf文件的修改
user root;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_rlimit_nofile 655350;
events {
worker_connections 36000;
}