ngx_http_upstream

This module provides simple load-balancing (round-robin and client IP) across backend servers.

Examples

upstream  backend  {
    server   backend1.example.com       weight=5;
    server   backend2.example.com:8080;
    server   unix:/tmp/backend3;
}

server {
    location / {
        proxy_pass  http://backend;
    }
}

Directives

Variables

Since version 0.5.18, it is possible to log via log_module following variables:

Configuration example:

log_format timing '$remote_addr - $remote_user [$time_local] $request '
                  'upstream_response_time $upstream_response_time '
                  'msec $msec request_time $request_time';

log_format up_head '$remote_addr - $remote_user [$time_local] $request '
                   'upstream_http_content_type $upstream_http_content_type';

ip_hash

syntax: ip_hash

default: none

context: upstream

This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.

It is not possible to combine ip_hash and weight methods for connection distribution. If one of the servers must be removed for some time, you must mark that server as *down*.

For example:

upstream  backend  {
    ip_hash;

    server   backend1.example.com;
    server   backend2.example.com;
    server   backend3.example.com  down;
    server   backend4.example.com;
}

server

syntax: server name [parameters]

default: none

context: upstream

Directive assigns the name and the parameters of server. For the name it is possible to use a domain name, an address, port or unix socket. If domain name resolves to several addresses, then all are used.

Example configuration:

upstream  backend  {
    server   backend1.example.com    weight=5;
    server   127.0.0.1:8080          max_fails=3  fail_timeout=30s;
    server   unix:/tmp/backend3;
}

upstream

syntax: upstream name { ... }

default: none

context: http

This directive describes a set of servers, which can be used in directives proxy_pass and fastcgi_pass as a single entity. They can listen to server on different ports and furthermore, it is possible to simultaneously use a server that listens on both TCP and Unix sockets.

Servers can be assigned different weights. If not specified weight is equal to one.

Example configuration:

upstream  backend  {
    server   backend1.example.com    weight=5;
    server   127.0.0.1:8080          max_fails=3  fail_timeout=30s;
    server   unix:/tmp/backend3;
}

Requests are distributed according to the servers in round-robin manner with respect of the server weight.
For example of every 7 seven requests given above they will be distributed like this: 5 requests on backend1.example.com and on request to the second and the third of server. If with an attempt at the work with the server error occurred, then the request will be transmitted to the following server and then until all workers of server not are tested. If successful answer is not succeeded in obtaining from all servers, then to client will be returned the result of work with the last server.

References

Original Documentation

NginxHttpUpstreamModule (last edited 2008-04-09 14:05:13 by DenisBraekhus)