Create an nginx load balancer

I wanted to play with a simple load balancer with powerful implications.  A load balancer using nginx as the front end and two apache servers on the backend.  All using CentOS 6 minimal installations.

server1 - apache -

yum -y install httpd
echo server1 /var/www/html/index.html
lokkit -s http -s https
chkconfig httpd on && service httpd restart

server2 - apache -

yum -y install httpd
echo server2 /var/www/html/index.html
lokkit -s http -s https
chkconfig httpd on && service httpd restart

load balancer - nginx -

Add the nginx repository

vi /etc/yum.repos.d/nginx.repo

Copy and past the following into the nginx.repo file and save it.

name=nginx repo

Install nginx

yum install -y nginx
service nginx start
lokkit -s http -s https

Modifiy the default.conf file.

vi /etc/nginx/conf.d/default.conf

Add to the top the upstream section and under location below the two proxy* lines.

upstream web_backend {
#       ip_hash;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://web_backend;

Restart nginx.

service nginx restart


Using your favorite web-browser, type in and refresh the page a few times to see the text change from server1 to server2.