1

I want to get the currently open file descriptors and upper limit of open file descriptors on an AWS Linux instance. The reason is to determine whether to increase the limits for nginx to have enough file descriptors available.

I get the following outputs:

$ulimit -a
open files                      (-n) 1024

$lsof | wc -l
397

$sudo lsof | wc -l
1870

$sysctl fs.file-nr
fs.file-nr = 1248   0   197688

From this answer I read that sysctl is a hard limit of memory allocation for file descriptors in the kernel, while ulimit seems to be a limit that can apply to specific domains and is per process with only a session scope.

Do I interpret the values above correctly that:

  1. ulimit shows that nginx can open 1024 files per process. nginx runs 2 worker processes on that machine, so it has 2048 file descriptors available.

  2. lsof shows that the processes of the logged in user (who is running nginx) had 397 files open.

  3. sudo lsof shows that systemwide there were 1870 files open at that moment.

  4. sysctl shows that the systemwide maximum number of file descriptors is 197688. Meaning that across all users and all processes running on that machine, they cannot have more than 197688 files open.

  5. The difference in currently open files 1248 vs. 1870 is because they are counted differently. file-nr ignores some of the directories which are considered as files by lsof. https://unix.stackexchange.com/q/176967/370277

Adapting the limits for nginx:

  • Nginx amplify currently shows nginx.workers.fds_count around 300 with rare peaks up to 900. That value should display the sum of both worker processes. That means even considering peaks, nginx does not use half of the available 2048 file descriptors. Are we fine here?

  • The nginx configuration nginx -T shows worker_connections 1024, which seems to be the number of connections per worker process that can be opened. Each worker needs 2 file descriptors per connection. So the limit here is 1024 worker_connections * 2 file descriptors per connection * 2 worker processes = 4096 file descriptors. But we could never reach that because both nginx processes only have 2048 file descriptors available (according to point 1 above). Is that correct?

  • worker_rlimit_nofile is not set anywhere, and I didn't find a default value in the docs. So is there no limit applied? In other words, if worker_rlimit_nofile = worker_connections *2 as described here, why is possible to set both values in the nginx config, if one values determined the other value anyway?

Manuel
  • 225
  • 3
  • 13

0 Answers0