proxy_next_upstream

The `proxy_next_upstream` directive controls whether a request should be passed to the next upstream server on failure.

Syntaxproxy_next_upstream parameter1 [parameter2 ...];
Defaultoff
Contexthttp, server, location
Arguments1+

Description

The proxy_next_upstream directive in NGINX is critical for managing failures and ensuring high availability when using the proxy module. It defines conditions under which the current proxied request will be retried on the next server in the upstream group. This is particularly useful for load balancing and fault tolerance in microservices architectures where a single point of failure could be mitigated by rerouting traffic to another server.

This directive accepts one or more arguments, which specify the error conditions that will trigger a pass to the next upstream server. Common parameters include 'error', 'timeout', 'invalid_header', and 'failed', among others. By combining different parameters, administrators can fine-tune the conditions for retrying a request, optimizing user experience by reducing the likelihood of a failed request due to transient issues on a single upstream server.

When an upstream server fails for one of the specified reasons, NGINX will attempt to contact the next server in the configured upstream block. This helps to prevent downtime and provides seamless failover. It's essential for maintaining service continuity, as clients remain unaware of the underlying server failures, assuming the system is more reliable than it is. However, it's important to implement proper monitoring and alerting to identify issues as they arise, despite the automatic retries enabled by this directive.

Config Example

upstream myapp {
    server backend1.example.com;
    server backend2.example.com;
}

location / {
    proxy_pass http://myapp;
    proxy_next_upstream error timeout invalid_header;
}

Overusing this directive can lead to increased latency, as NGINX waits for retries before responding to clients.

Ensure that the upstream servers have compatibility, as retries especially can lead to unexpected behavior if different versions or configurations are used.

← Back to all directives