TERMINOLOGIES of NGINX
Load-balancing is a useful mechanism to distribute incoming traffic around several capable Virtual Private servers. By apportioning the processing mechanism to several machines, redundancy is provided to the application — ensuring fault tolerance and heightened stability. The Round Robin algorithm for load balancing sends visitors to one of a set of IPs. At its most basic level Round Robin, which is fairly easy to implement, distributes server load without implementing considering more nuanced factors like server response time and the visitors’ geographic region.
Upstream Module
In order to set up a round robin load balancer, we will need to use the nginx upstream module. We will incorporate the configuration into the nginx settings.
Go ahead and open up your website’s configuration (in my examples I will just work off of the generic default virtual host):
sudo nano /etc/nginx/sites-available/default
We need to add the load balancing configuration to the file.
First we need to include the upstream module which looks like this:
upstream backend { server backend1.example.com; server backend2.example.com; server backend3.example.com; }
We should then reference the module further on in the configuration:
server { location / { proxy_pass http://backend; } }
Restart nginx:
sudo service nginx restart
As long as you have all of the virtual private servers in place you should now find that the load balancer will begin to distribute the visitors to the linked servers equally.
Directives
The previous section covered how to equally distribute load across several virtual servers. However, there are many reasons why this may not be the most efficient way to work with data. There are several directives that we can use to direct site visitors more effectively.
Weight
One way to begin to allocate users to servers with more precision is to allocate specific weight to certain machines. Nginx allows us to assign a number specifying the proportion of traffic that should be directed to each server.
A load balanced setup that included server weight could look like this:
upstream backend { server backend1.example.com weight=1; server backend2.example.com weight=2; server backend3.example.com weight=4; }
The default weight is 1. With a weight of 2, backend 2.example will be sent twice as much traffic as backend 1, and backend 3, with a weight of 4, will deal with twice as much traffic as backend 2 and four times as much as backend 1.
Hash
IP hash allows servers to respond to clients according to their IP address, sending visitors back to the same VPS each time they visit (unless that server is down). If a server is known to be inactive, it should be marked as down. All IPs that were supposed to routed to the down server are then directed to an alternate one.
The configuration below provides an example:
upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; }
Max Fails
According to the default round robin settings, nginx will continue to send data to the virtual private servers, even if the servers are not responding. Max fails can automatically prevent this by rendering unresponsive servers inoperative for a set amount of time.
There are two factors associated with the max fails: max_fails and fall_timeout. Max fails refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive. Fall_timeout specifies the length of that the server is considered inoperative. Once the time expires, new attempts to reach the server will start up again. The default timeout value is 10 seconds.
A sample configuration might look like this:
upstream backend { server backend1.example.com max_fails=3 fail_timeout=15s; server backend2.example.com weight=2; server backend3.example.com weight=4; }
Defining an Upstream Context for Load Balancing Proxied Connections
In the previous examples, we demonstrated how to do a simple http proxy to a single backend server. Nginx allows us to easily scale this configuration out by specifying entire pools of backend servers that we can pass requests to.
We can do this by using the upstream
directive to define a pool of servers. This configuration assumes that any one of the listed servers is capable of handling a client’s request. This allows us to scale out our infrastructure with almost no effort. The upstream
directive must be set in the http context of your Nginx configuration.
Let’s look at a simple example:
# http context
upstream backend_hosts {
server host1.example.com;
server host2.example.com;
server host3.example.com;
}
server {
listen 80;
server_name example.com;
location /proxy-me {
proxy_pass http://backend_hosts;
}
}
In the above example, we’ve set up an upstream context called backend_hosts
. Once defined, this name will be available for use within proxy passes as if it were a regular domain name. As you can see, within our server block we pass any request made to example.com/proxy-me/...
to the pool we defined above. Within that pool, a host is selected by applying a configurable algorithm. By default, this is just a simple round-robin selection process (each request will be routed to a different host in turn).
Changing the Upstream Balancing Algorithm
You can modify the balancing algorithm used by the upstream pool by including directives or flags within the upstream context:
- (round robin): The default load balancing algorithm that is used if no other balancing directives are present. Each server defined in the upstream context is passed requests sequentially in turn.
least_conn
: Specifies that new connections should always be given to the backend that has the least number of active connections. This can be especially useful in situations where connections to the backend may persist for some time.ip_hash
: This balancing algorithm distributes requests to different servers based on the client’s IP address. The first three octets are used as a key to decide on the server to handle the request. The result is that clients tend to be served by the same server each time, which can assist in session consistency.hash
: This balancing algorithm is mainly used with memcached proxying. The servers are divided based on the value of an arbitrarily provided hash key. This can be text, variables, or a combination. This is the only balancing method that requires the user to provide data, which is the key that should be used for the hash.
Deconstructing a Basic HTTP Proxy Pass
The most straight-forward type of proxy involves handing off a request to a single server that can communicate using http. This type of proxy is known as a generic “proxy pass” and is handled by the aptly named proxy_pass
directive.
The proxy_pass
directive is mainly found in location contexts. It is also valid in if
blocks within a location context and in limit_except
contexts. When a request matches a location with a proxy_pass
directive inside, the request is forwarded to the URL given by the directive.
Let’s take a look at an example:
# server context
location /match/here {
proxy_pass http://example.com;
}
. . .
In the above configuration snippet, no URI is given at the end of the server in the proxy_pass
definition. For definitions that fit this pattern, the URI requested by the client will be passed to the upstream server as-is.
For example, when a request for /match/here/please
is handled by this block, the request URI will be sent to the example.com
server as : http://example.com/match/here/please
.