Mastering Nginx

(Ron) #1
Chapter 4

[ 73 ]

The upstream module


Closely paired with the proxy module is the upstream module. The upstream


directive starts a new context, in which a group of upstream servers is defined.
These servers may be given different weights (the higher the weight, the greater


the number of connections NGINX will pass to that particular upstream server),
may be of different types (TCP versus UNIX domain), and may even be marked


as down for maintenance reasons.


The following table summarizes the directives valid within the upstream context:


Table: Upstream module directives

Directive Explanation
ip_hash Ensures the distribution of connecting clients evenly over
all servers by hashing the IP address, keying on its class-C
network.
keepalive The number of connections to upstream servers that
are cached per worker process. When used with HTTP
connections, proxy_http_version should be set to 1.1
and proxy_set_header to Connection "".
least_conn Activates the load-balancing algorithm where the server
with the least number of active connections is chosen for
the next new connection.
server Defines an address (domain name or IP address with an
optional TCP port, or path to a UNIX-domain socket)
and optional parameters for an upstream server. The
parameters are:


  • weight: It sets the preference for one server over
    another

  • max_fails: It is the maximum number of
    unsuccessful communication attempts to a server
    within fail_timeout before the server is marked
    as down

  • fail_timeout: It is the length of time a server
    has to respond to a request and the length of time
    a server will be marked as down

  • backup: It will only receive requests once the
    other servers are down

  • down: It marks a server as not able to
    process requests

Free download pdf