Tips for maximizing Nginx requests/sec?

Solution 1:

You're abusing Nginx's worker_threads. There is absolutely no need to run that many workers. You should run as many workers as you have CPUs and call it a day. If you're running gunicorn on the same server, you should probably limit nginx workers to two. Otherwise, you're just going to thrash the CPUs with all the context switching required to manage all of those processes.

Solution 2:

I have used nginx to serve 5K request a second for static content. You can increase the number of worker_connections which are currently set to 1024.

The max_client calculation would be as follows.

The worker_connections and worker_proceses from the main section allows you to calculate maxclients value:

max_clients = worker_processes * worker_connections

In a reverse proxy situation, max_clients becomes

max_clients = worker_processes * worker_connections/4

http://wiki.nginx.org/EventsModule#worker_connections

Calculating the max worker connections is easy once you know the capacity of your setup. Total capacity/number of core is max worker connections. To calculate total capacity there are multiple ways.

  1. I would suggest you try and benchmark your setup that will give you the most realistic numbers. You can use tools like siege, pummel, apache bench etc, do remember to measure system resource usage at during the test.

If you the above method doesn't work for you then try the methods below. I am doing broad assumptions ignoring RAM and IO, they will also factor in but these will give you starting points and you can make adjustments from there on.

  1. Assumption that bandwidth is the bottleneck, take the average object size that nginx is serving and divide your bandwidth with that and you'll get the max supported qps.

  2. In the second assumption, CPU is the bottleneck. In this case measure the request time and divide 1 by it and multiple with the number of cores in your system. This will give the number of request per second nginx can handle.