Linux最大文件打開數
介紹
在Linux下有時會遇到Socket/File : Can't open so many files的問題。其實Linux是有文件句柄限制的,而且Linux默認一般都是1024(阿里雲主機默認是65535)。在生產環境中很容易到達這個值,因此這里就會成為系統的瓶頸。
1.查看方法
使用ulimit -a 或者 ulimit -n
open files (-n) 1024 是linux操作系統對一個進程打開的文件句柄數量的限制(也包含打開的套接字數量)
這里只是對用戶級別的限制,其實還有個是對系統的總限制,查看系統總線制:
# cat /proc/sys/fs/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調用,設置每個進程的限制。如果得到大量使用完文件句柄的錯誤信息,是應該增加這個值。
也就是說,這項參數是系統級別的。
2.修改方法
臨時生效:
# ulimit -SHn 10000
其實ulimit 命令身是分軟限制和硬限制,加-H就是硬限制,加-S就是軟限制。默認顯示的是軟限制,如果運行ulimit 命令修改時沒有加上-H或-S,就是兩個參數一起改變。
軟限制和硬限制的區別?
硬限制就是實際的限制,而軟限制是警告限制,它只會給出警告。
永久生效
要想ulimits 的數值永久生效,必須修改配置文件/etc/security/limits.conf
在該配置文件中添加
* hard nofile 65535
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
* 表示所用的用戶
修改系統總限制
其實上的修改都是對一個進程打開的文件句柄數量的限制,我們還需要設置系統的總限制才可以。
假如,我們設置進程打開的文件句柄數是1024 ,但是系統總線制才500,所以所有進程最多能打開文件句柄數量500。從這里我們可以看出只設置進程的打開文件句柄的數量是不行的。所以需要修改系統的總限制才可以。
echo 6553560 > /proc/sys/fs/file-max
上面是臨時生效方法,重啟機器后會失效;
永久生效方法:
修改 /etc/sysctl.conf, 加入
fs.file-max = 6553560 重啟生效
Nginx 進程最大可打開文件數
worker_processes:操作系統啟動多少個工作進程運行Nginx。注意是工作進程,不是有多少個nginx工程。在Nginx運行的時候,會啟動兩種進程,一種是主進程master process;一種是工作進程worker process。例如我在配置文件中將worker_processes設置為4,啟動Nginx后,使用進程查看命令觀察名字叫做nginx的進程信息,我會看到如下結果:
-
[root@localhost nginx] # ps -elf | grep nginx
-
4 S root 2203 2031 0 80 0 - 46881 wait 22:18 pts/0 00:00:00 su nginx
-
4 S nginx 2204 2203 0 80 0 - 28877 wait 22:18 pts/0 00:00:00 bash
-
5 S root 2252 1 0 80 0 - 11390 sigsus 22:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-
5 S nobody 2291 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2292 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2293 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2294 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
0 R root 2312 2299 0 80 0 - 28166 - 22:24 pts/0 00:00:00 grep --color=auto nginx
1個nginx主進程,master process;還有四個工作進程,worker process。主進程負責監控端口,協調工作進程的工作狀態,分配工作任務,工作進程負責進行任務處理。一般這個參數要和操作系統的CPU內核數成倍數。
worker_connections:這個屬性是指單個工作進程可以允許同時建立外部連接的數量。無論這個連接是外部主動建立的,還是內部建立的。這里需要注意的是,一個工作進程建立一個連接后,進程將打開一個文件副本。所以這個數量還受操作系統設定的,進程最大可打開的文件數有關。
設置Nginx進程最大可打開文件數
1、更改操作系統級別的“進程最大可打開文件數”的設置
2、更改Nginx軟件級別的“進程最大可打開文件數”的設置
剛才更改的只是操作系統級別的“進程最大可打開文件”的限制,作為Nginx來說,我們還要對這個軟件進行更改。打開nginx.conf主配置文件。您需要配合worker_rlimit_nofile屬性。如下:
user root root;
worker_processes 4;
worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
請注意代碼行中加粗的兩個配置項,請一定兩個屬性全部配置。
驗證Nginx的“進程最大可打開文件數”是否起作用
那么我們如何來驗證配置是否起作用了呢?在linux系統中,所有的進程都會有一個臨時的核心配置文件描述,存放路徑在 /pro/進程號/limit。
首先我們來看一下,沒有進行參數優化前的進程配置信息:
-
[root@localhost nginx] # ps -elf | grep nginx
-
4 S root 2203 2031 0 80 0 - 46881 wait 22:18 pts/0 00:00:00 su nginx
-
4 S nginx 2204 2203 0 80 0 - 28877 wait 22:18 pts/0 00:00:00 bash
-
5 S root 2252 1 0 80 0 - 11390 sigsus 22:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-
5 S nobody 2291 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2292 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2293 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
5 S nobody 2294 2252 0 80 0 - 11498 ep_pol 22:23 ? 00:00:00 nginx: worker process
-
0 R root 2318 2299 0 80 0 - 28166 - 22:42 pts/0 00:00:00 grep --color=auto nginx
可以看到,nginx工作進程的進程號是:2291 2292 2293 2294。我們選擇一個進程,查看其核心配置信息:
-
[root@localhost nginx]# cat /proc/ 2291/limits
-
Limit Soft Limit Hard Limit Units
-
Max cpu time unlimited unlimited seconds
-
Max file size unlimited unlimited bytes
-
Max data size unlimited unlimited bytes
-
Max stack size 8388608 unlimited bytes
-
Max core file size 0 unlimited bytes
-
Max resident set unlimited unlimited bytes
-
Max processes 3829 3829 processes
-
Max open files 1024 4096 files
-
Max locked memory 65536 65536 bytes
-
Max address space unlimited unlimited bytes
-
Max file locks unlimited unlimited locks
-
Max pending signals 3829 3829 signals
-
Max msgqueue size 819200 819200 bytes
-
Max nice priority 0 0
-
Max realtime priority 0 0
-
Max realtime timeout unlimited unlimited us
請注意其中的Max open files ,分別是1024和4096。那么更改配置信息,並重啟Nginx后,配置信息就是下圖所示了:
-
[root@localhost conf]# cat /proc/ 2351/limits
-
Limit Soft Limit Hard Limit Units
-
Max cpu time unlimited unlimited seconds
-
Max file size unlimited unlimited bytes
-
Max data size unlimited unlimited bytes
-
Max stack size 8388608 unlimited bytes
-
Max core file size 0 unlimited bytes
-
Max resident set unlimited unlimited bytes
-
Max processes 3829 3829 processes
-
Max open files 65535 65535 files
-
Max locked memory 65536 65536 bytes
-
Max address space unlimited unlimited bytes
-
Max file locks unlimited unlimited locks
-
Max pending signals 3829 3829 signals
-
Max msgqueue size 819200 819200 bytes
-
Max nice priority 0 0
-
Max realtime priority 0 0
-
Max realtime timeout unlimited unlimited us