Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In nginx, what is the relationship between worker_connections, keepalive_timeout and $connection

Tags:

nginx

The nginx documentation says

max_clients = worker_processes * worker_connections

but how does the keepalive factor into this? I have my configuration setup with 2 worker_processes and 8192 worker_connections; that means I can theoretically handle a maximum of 16384 concurrent connections. Pushing out 16384 streams of data concurrently is ginormous, but if I have a 60s keepalive_timeout then with each client hogging a connection for 1 minute that number has a completely different meaning. Which is it?

Connected to all this is the $connection variable that can be used with the log_format directive. I defined the following log format so I could analyze the server's performance:

log_format  perf  '$request_time $time_local $body_bytes_sent*$gzip_ratio $connection $pipe $status $request_uri';

That $connection variable is reporting around 11-12 million connections! I'm no math major, but obviously that number is way higher than worker_processes * worker_connections. So what is it supposed to represent?

In short, I'm trying to figure out how to determine a good value for worker_connection.

like image 698
Daniel Avatar asked Aug 11 '09 06:08

Daniel


2 Answers

$connection is a counter, not the total number of used connections right now. So it's intended to grow.

Keepalive connections cannot be discarded, so the room is worker_processes * worker_connections - keepalive connections

like image 61
rzab Avatar answered Nov 16 '22 01:11

rzab


just imagine the whole picture: first client connects to you, gets a file and then browser keeps connection for 60 secs. another client connects, gets, and keeps its connection too. at the end of firstr minute, you may have (in worst case) all the clients requested something from you in the last 60 secs still keeping their connections open

so, in the worst case you will serve "worker_processes * worker_connections / keep_alive" connections in a second, i.e. about 260 for your numbers. if you need more, you should alloc more connections - just for serving keepalives: read my answerr in Tuning nginx worker_process to obtain 100k hits per min

afaik nginx may hold 10k of inactive (keepalived) connections in 2.5mb of memory, so increasing worker_connections is cheap, very cheap. i think that main bottleneck here may be your OS itself

like image 23
Bulat Avatar answered Nov 16 '22 01:11

Bulat