First we must understand what is Load Balancing.
Wikipedia definition - >
"Load balancing is a computer networking method for distributing workloads across multiple computing resources, such as computers, a computer cluster, network links, central processing units or disk drives. Load balancing aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any one of the resources. Using multiple components with load balancing instead of a single component may increase reliability through redundancy. Load balancing is usually provided by dedicated software or hardware, such as a multilayer switch or a Domain Name System server process."
In layman terms wrt nginx , it is a way in which multiple servers are ready to serve a request from client to increase reliability and reduce disk access latency, etc . Having a load balancer allows us to dynamically add and remove back-end servers easily and perform many more functions listed here:
http://en.wikipedia.org/wiki/Load_balancing_(computing)#Load_balancer_features
This diagram depicts the scenario:
Source : http://indonetworksecurity.com/ |
Nginx is an amazing load balancer and used by many sites (wikipedia itself - link) .
In this diagram the load balancer can be running nginx and the servers can be running apache, boa or nginx itself.
So we shall now setup a very simple but effective load balancer using nginx (as load balancer) and any 2 backend servers (can be any webserver)
I have assumed you followed previous post in setting up nginx. [Build and Setup Nginx from Source]
In the conf folder there is a nginx.conf file and this is main configuration file for nginx.
under http directive ->
create a upstream directive ->
upstream backendserver { server backendserver1; server backendserver2; }
backend1/2 - > can be an ip/url of the server residing on the same system as nginx(localhost/127.0.0.1) or in another networked system as well.
under server directive inside http
create a location directive ->
location / { proxy_pass http://backendserver; }
note the same name backendserver in proxypass and upstream.
restart nginx if running and now a simple load balancer is up and running in round robin distribution.(make sure backendservers are up as well :P)
Advanced Configurations :
ip hash; Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses.
weight; Nginx allows us to assign a number specifying the proportion of traffic that should be directed to each server.
health_check; will send “
/
” requests to each server in the backend
group every five seconds. If any communication error or timeout occurs, or a proxied server responds with the status code other than 2xx or 3xx, the health check will fail, and the server will be considered unhealthy. Client requests are not passed to unhealthy servers.and many more.
Sources :
http://en.wikipedia.org/wiki/Load_balancing_(computing)
http://wiki.nginx.org/HttpUpstreamModule
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
https://www.digitalocean.com/community/articles/how-to-set-up-nginx-load-balancing
No comments:
Post a Comment