Overview

In this post, we will cover the following points

  • What is Nginx?
  • Install Nginx
  • Configuring Nginx as a load balancer
What is Nginx?

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.

Install Nginx
######### Debian #########
sudo nano /etc/apt/sources.list

# Add the following to the end of the list
deb http://nginx.org/packages/debian/ jessie nginx
deb-src http://nginx.org/packages/debian/ jessie nginx

# Save the file, exit the editor and then run the following commands
sudo aptitude update
sudo aptitude install nginx


######### Ubuntu  #########
nginx=stable
sudo add-apt-repository ppa:nginx/$nginx
sudo apt-get update
sudo apt-get install nginx


######### CentOS #########
sudo vi /etc/yum.repos.d/nginx.repo

# Enter the following to the file
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

#Save and exit, then use the command below
sudo yum update
sudo yum install nginx

Once done start serevr using below command

# To start server
sudo service nginx start

# To stop server
sudo service nginx stop

# To restart server
sudo service nginx restart

# To test if config are correct
sudo nginx -t

If all goes well, in a browser hit http://localhost You should see something like this

Configuring Nginx as a load balancer

We are going to add 2 server details running on localhost and at 8080 and 9090 ports.

Please note I am running this setup on my local machine so using localhost. In general, we will replace localhost with some IP.

  • Edit  nginx.conf and add following lines in http section
upstream customurl {
	server localhost:8080;
	server localhost:9090;
}

The whole block looks like this

ubuntu$ sudo nano /etc/nginx/nginx.conf

#block 

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##


	upstream localhost {
		server localhost:8080;
		server localhost:9090;
	}

...
  • We need to add proxy URL as customurl which was added in nginx.conf
ubuntu$ sudo nano /etc/nginx/sites-enabled/default

 # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #  try_files $uri $uri/ =404;

				# Add URL
                proxy_pass http://customurl;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
  • Make sure URL customurl is configured with DNS. Eg I have added URL in /etc/hosts to map to IP
cat /etc/hosts


127.0.0.1	customurl
127.0.1.1	ecode-HP-Laptop

...
  • Restart your Nginx server.
  • Now in a browser try http//:customurl .
  • It should redirect to any one server which we configured.
  • Now, shut down one of the servers. Hit refresh
  • The browser will still redirect to your server. That’s Load balancing in Action.

 

Categories: Nginx

0 Comments

Leave a Reply

Your email address will not be published.