7

高并发/高io下,设置linux下文件打开数限制

 1 year ago
source link: https://blog.star7th.com/2022/09/2526.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

高并发/高io下,设置linux下文件打开数限制

我的 https://www.dfyun.com.cn/ 服务是高并发的密集io型服务,连接数和打开文件数特别高。而在linux中,一切皆文件,所以连接数本身也会表现为文件打开数。默认情况下,linux单进程打开文件数的限制是1024 , 非常低,在密集io型服务里很容易超过。

一般的情况下,我们是根据服务器内存大小,以及单进程使用情况,来多开进程,同时限制单进程打开的文件数。但 dfyun 比较特殊——它是文件打开数特别多(连接数+本地文件打开数+遍历扫描文件操作等等) ,但占用的内存特别小。所以,我不需要根据内存来斟酌打开数值了,直接设置一个非常大的数据即可,让系统不再在文件数上做限制。
下面将直接上代码,放开linux设置,让其不再限制文件打开数。一些代码的细节可以看注释,如果有疑问,可直接上网查找该命令的概念。

# vi /etc/sysctl.conf ,末尾添加
fs.file-max = 655350000
fs.nr_open = 655350000

# 执行命令生效
sysctl -p
# 查看 max-file:
cat /proc/sys/fs/file-max
# 查看当前系统总打开文件数
cat /proc/sys/fs/file-nr
# 输出: 9344 0 592026,
# 分别为:1.已经分配的文件句柄数,2.已经分配但没有使用的文件句柄数,3.最大文件句柄数。
# 修改系统对单个进程打开的文件数限制
ulimit -n 655350000
echo "* soft nofile 655350000" >> /etc/security/limits.conf
echo "* hard nofile 655350000" >> /etc/security/limits.conf
# 顺便附上查看各个进程打开的文件数的命令。显示格式是文件数 + 进程id
lsof -n |awk '{print $2}'|sort|uniq -c|sort -nr|more

# 如果有安装nginx做转发,则还需要设置nginx 
#打开nginx本身的文件数限制
vi /etc/nginx/nginx.conf
# 找到相应位置覆盖修改这个配置
worker_rlimit_nofile 655350000;
events  {
  worker_connections 65535;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK