by Oliver
24. June 2013 23:08
worker_processes 1; # http://nginx.org/en/docs/ngx_core_module.html#worker_processes
Thanks to http://www.theunixtips.com/how-to-find-number-of-cpus-on-unix-system for helping me find out how many processor cores our VPS is using.
Fixing the painful "24: Too many open files" error in NGINX
When this error appears in your /var/etc/nginx/error.log file, you really want to do something about it. It basically tells you that some of your users are not being served content but instead receiving HTTP 500 errors generated by NGINX. Not a good thing!
To investigate the open file descriptors on your linux (we're running NGINX on Ubuntu 12.04), I followed advice from this post: Linux: Find Out How Many File Descriptors Are Being Used. Then, to fix the low limits I found, 1024 and 4096 for the soft and hard limits, respectively, the post Set ulimit parameters on ubuntu provided a good walkthrough to changing those limits persistently, i.e. even after a reboot. But I somehow had the feeling that in case of NGINX there had to be a simpler solution. Turns out, there is.
Let me introduce you to: worker_rlimit_nofile
This thread in the NGINX forums contained the hint I needed: Re: Handling nginx's too many open files even I have the correct ulimit. I had actually posted to that thread before and received the helpful answer over a month ago, but I somehow hadn't got around to implementing it. Until today. So here's what you need to do:
- Set the worker_rlimit_nofile (in the main context) to something significant (I used 65536 = 2^16) instead of the default 1024 as soft and 4096 as the hard limit.
- Also set worker_connections to the same number to allow for a lot of parallel connections, both from clients and to upstream servers.
Your nginx.conf file should now contain these lines:
1: user www-data;
2: worker_processes 1;
3: worker_rlimit_nofile 65536;
4: pid /var/run/nginx.pid;
5:
6: events {
7: worker_connections 65536; ## default: 1024
8: # multi_accept on;
9: }
Reload NGINX after configuration change
In a shell, tell NGINX to reload the configuration. You don't need to restart the whole process to get a configuration change live. Just run:
/usr/sbin/nginx -s reload
or
service nginx reload
Now enjoy thousands and thousands of parallel connections to your proxy server :-)