OS limits and event settings
Understand the limits stack
File-descriptor limits are process-specific soft and hard limits. A worker cannot exceed its effective limit just because worker_connections is large. worker_rlimit_nofile requests a worker RLIMIT_NOFILE setting; the process must have permission to set the requested limit. The service manager, runtime and kernel can impose other restrictions. A shell ulimit describes that shell and descendants, not an unrelated running service. Inspect the lab process only, and do not change global settings.
ulimit -Snulimit -Hn# Linux, substitute the verified lab worker PID:cat /proc/"$LAB_WORKER_PID"/limitsRead a service and container inventory
For a systemd-managed lab service, LimitNOFILE is a per-process limit setting and not a total service descriptor budget. A cgroup v2 memory.max controls memory at the group level; memory.current reports current usage. cpu.max expresses quota and period, with max meaning no quota at that level. Ancestor limits can be stricter. In this synthetic inventory the worker count should not be chosen simply from the host CPU count; quota 100000 per 100000 microseconds corresponds to one CPU of time. Identify the bottleneck before editing.
Host logical CPUs: 8Lab service effective soft NOFILE: 512Lab cgroup cpu.max: 100000 100000Lab cgroup memory.max: 134217728Lab cgroup memory.current: 50331648Choose an event mechanism and acceptance policy
Normally let Nginx choose the event mechanism. Linux commonly uses epoll; other systems may use different mechanisms. accept_mutex coordinates acceptance; it is not generally necessary when EPOLLEXCLUSIVE is supported or reuseport is used. multi_accept changes whether a worker accepts one or multiple new connections at a time (kqueue has separate semantics). reuseport creates separate listening sockets on supported platforms, allowing kernel distribution. Neither setting guarantees fair request distribution. This fragment is for a supported Linux lab, not a universal tuning recipe.
events { worker_connections 128; accept_mutex off; multi_accept off;}# in an HTTP server on supported Linux:listen 127.0.0.1:19442 reuseport;Treat affinity as an experiment
worker_cpu_affinity can bind workers to allowed CPU masks on supported platforms such as Linux and FreeBSD. A two-mask example places workers on logical CPUs 0 and 1 only if both are available in the lab cpuset. Do not copy the masks into a container with a different CPU set. Pinning is optional and may harm throughput when work is uneven. Use read-only inspection and compare controlled measurements before adopting a change. The syntax fixture deliberately omits affinity and explicit limit changes.
# Illustrative only: requires CPUs 0 and 1 in the allowed setworker_processes 2;worker_cpu_affinity 01 10;# Illustrative requested worker limit; requires permissionworker_rlimit_nofile 512;Practice and recap
An eight-CPU host runs a lab with one CPU-equivalent quota and a soft descriptor limit of 256. A proposed config uses eight workers, 4096 connections and CPU masks copied from another host. Identify three things to inspect before tuning. Check your reasoning against the manual exercise. Next: Backpressure and resource tuning.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
OS limits and event settings
Explain process and kernel limit interactions
Which limit should be inspected for the running lab worker?
What does a cgroup cpu.max of 100000 100000 indicate?
When is accept_mutex generally unnecessary?
What must be checked before applying CPU affinity masks?
What does worker_rlimit_nofile do?