Nginx – File descriptor limit in nginx/Passenger app

nginxphusion-passengerruby-on-railsUbuntu

We're running a Rails app in Passenger 4.0.45 and nginx 1.6.0 (installed by Passenger installer) running on Ubuntu 14.04. Under heavy load Passenger restarts all of the application processes. After enabling passenger debug logs, I found "Cannot accept client: Too many open files (errno=24)" errors in the Passenger logs.

Nginx is configured with "worker_rlimit_nofile 200000". Using cat /proc/pid/limits I'm able to confirm that nginx has the correct limits. Our Rails app running in Passenger, however, is not getting a higher limit.

I've added to /etc/security/limits.conf to give all users a high limit, and I've added session required pam_limits.so to both /etc/pam.d/common-session and /etc/pam.d/common-session-noninteractive` and restarted.

I can run

su appuser --shell /bin/bash --command "ulimit -n"

and I get a high number.

I finally tried setting the limit within the Rails app by adding the following to an initializer:

Process.setrlimit(Process::RLIMIT_NOFILE, 65535)

The result is:

Operation not permitted - setrlimit (Errno::EPERM)

Best Answer

I got some help from Phusion to solve this problem, so here's the solution I came up with. When nginx starts on Ubuntu, the init script looks for /etc/default/nginx and runs commands found there before actually starting nginx.

So (in addition to the limits settings above) adding /etc/default/nginx with the following contents:

ulimit -Hn 200000
ulimit -Sn 200000

and then restarting nginx, fixed the issue. This applies to nginx and all of the passenger processes, including PassengerHelperAgent and the Rails processes.