Websites that deal with huge traffic generally use a technique called load balancing to improve the availability and responsiveness of applications. A load balancer distributes incoming traffic among multiple servers hosting same content. The distribution of load is determined by various scheduler algorithms.
In this section, we will see how to add a load balancer in front of our application servers (assuming that they are running on the IPs 127.0.0.1
, 127.0.0.2
, and 127.0.0.3
on the port 9000
) using different HTTP web servers.
The Apache HTTP server provides a secure, efficient, and extensible server that supports HTTP services. The Apache HTTP server can be used as a load balancer through its mod_proxy
and mod_proxy_balance
modules.
To use Apache HTTP as a load balancer, mod_proxy
and mod_proxy_balancer
have to be present in the server. To set up the load balancer, all we need to do is update /etc/httpd/conf/httpd.conf
.
Let's update the configuration step by step:
VirtualHost
:<VirtualHost *:80> </VirtualHost>
VirtualHost
so that our server cannot be used for masking the identities of clients from the source servers:ProxyRequests off
BalanceMembers
. Also, if we want to use the round-robin strategy, we also need to set it as lbmethod
(load balancing method):<Proxy balancer://app> BalancerMember http://127.0.0.1:9000 BalancerMember http://127.0.0.2:9000 BalancerMember http://127.0.0.3:9000 ProxySet lbmethod=byrequests </Proxy>
Order Deny,Allow Deny from none Allow from all
ProxyPass / balancer://app/
The configuration that needs to be added to the Apache HTTP configuration file is as follows:
<VirtualHost *:80> ProxyPreserveHost On ProxyRequests off <Proxy balancer://app> BalancerMember http://127.0.0.1:9000 BalancerMember http://127.0.0.2:9000 BalancerMember http://127.0.0.3:9000 Order Deny,Allow Deny from none Allow from all ProxySet lbmethod=byrequests </Proxy> ProxyPass / balancer://app/ </VirtualHost>
To enable SSL, we will need to add the following code to the VirtualHost
definition:
SSLEngine on SSLCertificateFile /path/to/domain.com.crt SSLCertificateKeyFile /path/to/domain.com.key
For more information on Apache HTTP's mod_proxy
module, refer to http://httpd.apache.org/docs/2.2/mod/mod_proxy.html.
The nginx server is a high performance HTTP server and a reverse proxy as well. It is also an IMAP/POP3 proxy server. We can configure nginx to act as a load balancer using two modules—proxy
and upstream
. These two modules are part of the nginx core and are available by default.
The nginx configuration file, nginx.conf
, is generally located at /etc/nginx
. Let's update it to use nginx as a load balancer for our application step by step:
upstream
module for our cluster of application servers. The syntax is as follows:upstream <group> { <loadBalancingMethod>; server <server1>; server <server2>; }
The default load balancing method is round-robin. So, we need not specify it explicitly when we wish to use it. Now, for our application, the upstream
module will be as follows:
upstream app { server 127.0.0.1:9000; server 127.0.0.2:9000; server 127.0.0.3:9000; }
server
module's location module:server { listen 80 default_server; server_name localhost; … location / { proxy_pass http://app; proxy_set_header Host $host; } }
The nginx server also supports proxying WebSocket. To enable WebSocket connections, we need to add two headers to the location
module. So, if our Play application uses WebSocket, we can define the location
module as follows:
location / { proxy_pass http://app; proxy_set_header Host $host; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
To enable SSL, we need to add the following settings to the server definition:
ssl_certificate /path/to/domain.com.crt; ssl_certificate_key path/to/domain.com.key;
This configuration has been tested on nginx/1.4.7.
Refer to the nginx documentation at http://nginx.org/en/docs/http/load_balancing.html#nginx_load_balancing_configuration for more details.
The lighttpd
server is a lightweight web server designed and optimized for high performance environments. All the utilities that may be required are available as modules and can be included as per our requirements. We can set lighttpd
as a frontend server for our Play application using the mod_proxy
module. We need to make a few configuration changes to achieve this. They are as follows:
lighttpd.conf
file (generally located at /etc/lighttpd/
) to load additional modules.include "modules.conf"
modules.conf
(located in the same directory as lighttpd.conf
) to load the mod_proxy
module.mod_access
is enabled. Update server.modules
to the following code:server.modules = ( "mod_access", "mod_proxy" )
mod_proxy
by uncommenting this line:include "conf.d/proxy.conf"
proxy.conf
file (generally located at /etc/lighttpd/conf.d/
) with the server proxy configuration. The q
module has only three settings:proxy.debug
: This setting enables/disables the log levelproxy.balance
: This setting is a load balancing algorithm (round-robin, hash, and fair)proxy.server
: This setting is where requests are sentThe expected format of defining a proxy.server
setting is as follows:
( <extension> => ( [ <name> => ] ( "host" => <string> , "port" => <integer> ), ( "host" => <string> , "port" => <integer> ) ), <extension> => ... )
The terms in this code are explained as follows:
<extension>
: This term is the file extension or prefix (if started with "/"
); empty quotes, ""
, match all the requests<name>
:This term is the optional name that shows up in the generated statistics of mod_status
host
: This term is used to specify the IP address of the proxy serverport
: This term is used to set the TCP port on its corresponding host (the default value is 80
)server.modules += ( "mod_proxy" ) proxy.balance = "round-robin" proxy.server = ( "" => ( "app" => ( "host" => "127.0.0.1", "port" => 9000 ), ( "host" => "127.0.0.2", "port" => 9000 ), ( "host" => "127.0.0.3", "port" => 9000 ) ) )
This configuration has been tried on lighttpd/1.4.35 on March 12, 2014.
For more information on the configuration settings of mod_proxy
, refer to http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModProxy.
High Availability
Proxy (HAProxy) offers high availability, load balancing, and proxying for TCP and HTTP-based applications. We can set HAProxy as a load balancer by updating the haproxy.cfg
configuration file (it is generally located at /etc/haproxy/
).
Let's make the required configuration changes step by step:
backend <name> balance <load balance method> server <sname> <ip>:<port> server <sname> <ip>:<port>
backend app balance roundrobin server app1 127.0.0.1:9000 server app1 127.0.0.2:9000 server app1 127.0.0.3:9000
frontend main *:80 default_backend app
No additional configuration is required for an application using WebSockets.
18.118.10.32