nginx 优化系列之worker_connections

http://ddbiz.com/blog/web%E6%9C%8D%E5%8A%A1/nginx-%E4%BC%98%E5%8C%96%E7%B3%BB%E5%88%97%E4%B9%8Bworker_connections/153

nginx.conf

events {

use epoll;

worker_connections  8192;

}

增加 worker_connections,测试配置报告:

 

[warn]: 8192 worker_connections are more than open file resource limit: 1024

这需要调整系统的文件描述符 nofile

open file resource limit 是linux中process可以打开的文件句柄数量。增加这个数值需要调整两个配置:

第一步, 修改系统最大允许的文件描述符

查看当前的设置:

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

2390251

或者

$ sysctl -a

fs.file-max = 2390251

该系统是CentOS 5.x x64版本,安装好后没有做过优化设定, 2390251是其默认值

$ ulimit -n

1024

如果 /proc/sys/fs/file-max小于我们要设定的句柄数量,可以通过:

$echo “2390251″ > /proc/sys/fs/file-max

或者修改 /etc/sysctl.conf,在文件中修改fs.file-max的值(没有就创建一条)

修改完后可以通过

$ sysctl -p

使设置生效

2. 文件描述符修改后,需要调整针对用户或者组(user/group)的限制

/etc/security/limits.conf,文件格式为

<domain>  <type>  <item>  <value>

其中<type>为 soft或者hard,有些应用会自动把soft限制提升到hard限制,如java,至于nginx,没有看到相关的文档。

如:

* – nofile  8192

表示对所有的用户,文件描述符可以用到 8192, 或者

user_abc  - nofile  8192

表示对用户 user_abc,文件描述符可以用到 8192

3. 有些情况 /etc/security/limits.conf并不会发生作用,如在 init.d 中启用的进程,或者daemons运行的进程。

此时比较有效的办法是在init的script中,明确命令 ulimit -n xxxxx

除了在系统中进行设定 nofile(fs.file-max) 值外,可以在 nginx.conf 中指定worker_process可以使用的nofile值,如:
#user  nobody;
worker_processes  3;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        /var/run/nginx.pid;
worker_rlimit_nofile    8192;
events {
        use epoll;
        worker_connections  8192;
}
重新加载nginx配置,使新设定生效。
上面方案还临时减少了另外一个常常出现的错误:

limiting connections by zone “one”, client: xxx.xxx.xx.x, server:….

关于此问题的详细描述,会在后续文章中列出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值