Nginx worker Process & worker connections explained Events module
worker process = auto means
Equal to number of CPU cores available in the machine.
you can manually adjust it if you need lower usage of cpu. in multi application environment like docker.
Worker connections Default 512
in 2 core machine 2 worker process.
each worker process handles 1024*2 = 2048 connections / per seconds (1000+ live visitors).
enough cpu power then increase worker connection per seconds.
Bottleneck file descriptors limit in linux kernel
nginx multi_accept on |off;
default value off, accepts one connection at a time, with on accepts all connection at once, useful in traffic with scarifying cpu power.
mutex on | off;
mutual exclusion) to open the listening sockets
apache: process use memory, process switch uses cpu,
nginx: asynchronous, event‑driven approach has a problem
blocking: then thread pool introduced nginx 1.17.
instead of processing task by thread it will put in pool so another free thread can do this, threads requires resources.
to enable include aio threads directive
default off (not recommended for multi core cpus and cpus with hyperthreading)
worker_priority default 0
-20 to hgh
kernel process at -5
Defines the size of core files per worker process.
number of files that a worker process may use simultaneously.
maximum number of outstanding asynchronous I/O operations for a single worker process, if we use epoll connection.
epoll: An efficient method for Linux 2.6+ based operating systems.
Disable access_logs if don’t use
also read nginx error log & frequent errors
proxy (php -fpm , apache as a proxy to nginx)
you must read nginx conf explained here.
$request_time – from client to client
$upstream_connect_time – upstream connection
$upstream_header_time – connection + firstbyte
$upstream_response_time – connection +last byte
188.8.131.52 – – [31/Jul/2020:06:25:21 +0000] “GET /health/daily-nutritional-requirement/amp HTTP/1.1” 301 5 “-” “Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)”
access_log /var/log/nginx/access.log timed_combined;
184.108.40.206 – – [31/Jul/2020:06:25:21 +0000] “GET /health/daily-nutritional-requirement/amp HTTP/1.1” 301 5 “-” “Mozilla/5.0 (Linux; Android 7.0;) AppleWebKit/537.36 (KHTML, like Gecko) Mobile Safari/537.36 (compatible; PetalBot;+https://aspiegel.com/petalbot)” 0.640 0.640 .
0.640 secs google bot
FIle descriptors in /etc/security/limits.conf
sys.fs.file-max – The system‑wide limit
nofile – user limit
ulimit -Hn (hard limit we cannot increase more than this unless kernel config)
ulimit -Sn (sof limit we can increase upto hard limit)
root@instance-1:~# cat /proc/sys/fs/file-max
root@instance-1:~# ulimit -Sn
root@instance-1:~# ulimit -Hn
Connection Queue by Linux at etc/sysctl.conf
maximum number of connection queued for nginx. (default 512 nginx accepts very fast but required in traffic spike)
nginx stub status module.
rate at which packets are buffered by the network card before being handed off to the CPU
connection ques at operating system before nginx can process
512 to 65536
net.core.somaxconn = 65535 //Max connections
net.core.netdev_max_backlog = 65535 //incoming connections backlog