nginx打开的文件太多,似乎无法提高限制


22

服务器是Ubuntu 13.04(GNU / Linux 3.9.3-x86_64-linode33 x86_64)。

nginx是nginx / 1.2.6。

我已经为此工作了几个小时,所以这就是我得到的,这也是我所做的。

tail -f /usr/local/nginx/logs/error.log
2013/06/18 21:35:03 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:04 [crit] 3427#0: accept4() failed (24: Too many open files)
2013/06/18 21:35:05 [crit] 3426#0: accept4() failed (24: Too many open files)

Nginx运行:

geuis@localhost:~$ ps aux | grep nginx
root      3422  0.0  0.0  39292   380 ?        Ss   21:30   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    3423  3.7 18.8 238128 190848 ?       S    21:30   0:13 nginx: worker process      
nobody    3424  3.8 19.0 236972 192336 ?       S    21:30   0:13 nginx: worker process      
nobody    3426  3.6 19.0 235492 192192 ?       S    21:30   0:13 nginx: worker process      
nobody    3427  3.7 19.0 236228 192432 ?       S    21:30   0:13 nginx: worker process      
nobody    3428  0.0  0.0  39444   468 ?        S    21:30   0:00 nginx: cache manager process

在/etc/security/limits.conf中修改的软/硬限制(从文件末尾开始设置)

root soft  nofile 65536
root hard  nofile 65536

www-data soft nofile 65536
www-data hard nofile 65536

nobody soft nofile 65536
nobody hard nofile 65536

阅读最大文件

cat /proc/sys/fs/file-max
500000

在/etc/pam.d/common-session中:

session required pam_limits.so

添加了此选项,并很好地重启了服务器,对于Nginx,我通过获取父进程的PID来计算软限制/硬限制,并:

cat /proc/<PID>/limits
Limit                     Soft Limit           Hard Limit           Units     
Max open files            1024                 4096                 files     

父进程以“ root”身份运行,而4个工人以“ nobody”身份运行。

root      2765  0.0  0.0  39292   388 ?        Ss   00:03   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    2766  3.3 17.8 235336 180564 ?       S    00:03   0:21 nginx: worker process      
nobody    2767  3.3 17.9 235432 181776 ?       S    00:03   0:21 nginx: worker process      
nobody    2769  3.4 17.9 236096 181524 ?       S    00:03   0:21 nginx: worker process      
nobody    2770  3.3 18.3 235288 185456 ?       S    00:03   0:21 nginx: worker process      
nobody    2771  0.0  0.0  39444   684 ?        S    00:03   0:00 nginx: cache manager process

我已经尝试了一切我知道该怎么做的方法,并且能够从Google获得。我无法增加Nginx的文件限制。

救命?

Answers:


32

将以下行添加到您的nginx并重新启动该过程:

worker_rlimit_nofile 30000;

这将使工作人员可以处理更多文件。然后,您可以通过以下方式进行验证:

su - nobody
ulimit -Hn
ulimit -Sn

这应该输出新的硬/软限制。

参考


6
如果您仅更改worker_rlimit_nofileuWSGI设置而不是系统限制(这对我有用),则无法通过进行验证ulimit。相反,您应该直接查看/proc/<pid of worker>/limits
Jan Fabry 2014年

我认为该用户(没人/ www-data)必须注销并重新登录。重新启动服务器。ulimit对我来说表明它增加了,但是该过程仍然受到cat / proc / {pid} / limits的限制
-felix

@felix OP提到他们已经重新启动了服务器,但是是的,这是必需的。
弥敦道C

3

在Ubuntu中,编辑/etc/pam.d/su添加或取消注释所需的线路会话pam_limits.so

另外,请确保在/etc/security/limits.conf中,字符之间有TABS,不能有空格。


1

确保在编辑这些文件后运行以下命令

sysctl -p

然后重启nginx


1
没有效果。限制保持不变。
Geuis
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.