upstream

The `upstream` directive defines a group of backend servers for load balancing.

Syntaxupstream name { server address [parameter]; ... }
Defaultnone
Contexthttp
Argumentsblock (1)

Description

The upstream directive in NGINX creates a named block that can define multiple backend servers that can handle requests in a load-balanced manner. Within the upstream block, you can specify multiple server details, including their addresses and optional weights. NGINX supports various load balancing methods, such as round-robin, least connections, and IP hash, which can also be configured within the upstream context.

The syntax for the upstream directive allows for specifying multiple servers under a named context. For example, you can create an upstream group named backend that includes multiple server blocks, each representing different application instances. Each server can be defined with its IP address and optional parameters like max_fails and fail_timeout to control how NGINX handles server failures. Additionally, you can tune the load balancing method to suit your application's needs by specifying least_conn, ip_hash, etc.

The upstream directive does not have any default values; it must be defined explicitly in the configuration file. Once set up, it forms a reference point for other directives, such as proxy_pass, which can use this defined group to distribute incoming traffic across the specified backend servers effectively.

Config Example

upstream backend {
    server backend1.example.com;
    server backend2.example.com weight=2;
    server backend3.example.com max_fails=3 fail_timeout=30s;
}

Ensure that the upstream block is defined in the http context, as it is not valid in the server or location contexts.

Be cautious about the health checks and failover logic, as misconfiguration can lead to sending traffic to unresponsive servers.

Weighting in upstream blocks affects load distribution; ensure that it aligns with your expected load management.

← Back to all directives