Sei sulla pagina 1di 3

NGINX Plus as a Load Balancer

Installing NGINX Plus on Ubuntu


To install NGINX Plus on Ubuntu:

Create the /etc/ssl/nginx directory:

$ sudo mkdir /etc/ssl/nginx

$ cd /etc/ssl/nginx

Log in to NGINX Plus Customer Portal and download your nginx-repo.crt and nginx-repo.key files.

Copy the files to the /etc/ssl/nginx/ directory:

$ sudo cp nginx-repo.crt /etc/ssl/nginx/

$ sudo cp nginx-repo.key /etc/ssl/nginx/

Download the NGINX signing key from nginx.org and add it:

$ sudo wget https://nginx.org/keys/nginx_signing.key

$ sudo apt-key add nginx_signing.key

Install the apt-utils package and the NGINX Plus repository.

$ sudo apt-get install apt-transport-https lsb-release ca-certificates

$ printf "deb https://plus-pkgs.nginx.com/ubuntu `lsb_release -cs` nginx-plus\n" | sudo tee


/etc/apt/sources.list.d/nginx-plus.list

Download the 90nginx file to /etc/apt/apt.conf.d:

$ sudo wget -q -O /etc/apt/apt.conf.d/90nginx https://cs.nginx.com/static/files/90nginx

Update the repository information:

$ sudo apt-get update


Install the nginx-plus package. Any older NGINX Plus package is automatically replaced.

$ sudo apt-get install -y nginx-plus

HTTP Load Balancing

Proxying HTTP Traffic to a Group of Servers


Before start using Nginx Plus to load balance HTTP traffic to a group of servers, first, we need to
define the group with the upstream directive. The directive is placed in the http context.

Servers in the group are configured using the server directive. Let's see an example, the
following configuration defines a group named backend and consists of three server
configurations that may resolve in more than three actual servers.

http {  

    upstream backend {  

     server backend1.example.com ;  

 server backend2.example.com;  

         server 192.0.0.1 backup;  

     }  

}  

To pass the requests to a server group, the group name is specified in the proxy_pass directive. In the
below example, a virtual server running on Nginx passes all requests to the upstream backend group.

server {  

    location / {  

proxy_pass http://backend;  

    }  

}  
The following example combines the two snippets above and shows how to proxy HTTP request
to the backend server group. The group consists of three servers, two of the instances of the
same application while the third is a backup server. Because there is no load-balancing
algorithm is specified in the upstream block, Nginx uses the default algorithm, Round Robin.

http {  
    upstream backend {  
        server backend1.example.com;  
        server backend2.example.com;  
        server 192.0.0.1 backup;  
    }  
      
    server {  
        location / {  
            proxy_pass http://backend;  
        }  
    }  

Now NGINX Plus is ready to load balance the traffic. Please test and let me know if you stuck at
any point.

Potrebbero piacerti anche