zone
The 'zone' directive defines a shared memory zone for session state data in NGINX.
Description
The 'zone' directive is primarily used within the context of upstream and helps manage shared memory for variables used across multiple requests. It creates a shared memory zone that can be accessed by different NGINX processes, which is essential for storing session data or information about connections.
The directive takes one or two arguments: the name of the shared memory zone and an optional size. The allocated memory can store a variety of session-related data, which allows NGINX to handle features such as load balancing and health checks more intelligently. Each zone can be configured with a specific size to accommodate the expected number of sessions that may occur concurrently. If the size is too small, it may lead to unexpected behavior as sessions get dropped or improperly handled.
When defining a zone, NGINX will allocate the specified amount of memory from the system. The memory for the zone is maintained across worker processes, which allows session data to remain available and consistent, regardless of which worker handles the request. Therefore, the 'zone' directive is particularly useful for applications that require high availability and low latency, such as those involving sticky sessions, caching mechanisms, or other shared state scenarios.
Config Example
upstream backend {
zone my_zone 64k;
server backend1.example.com;
server backend2.example.com;
}Ensure the zone size is sufficient for your application's needs; a too-small zone can result in data loss.
Only one 'zone' directive should be defined per context to avoid conflicts.