Load balancing with Nginx — a quick experiment

Fathima Dilhasha
‘How to’ Guides by Dilhasha
4 min readJun 10, 2017

This post will discuss on how to setup a fault tolerant and highly stable server environment with your local servers(Virtual private servers). I will be using Nginx and WSO2 Application servers on a Ubuntu OS. I’ll be looking into the following.

  1. Setting up Nginx server locally
  2. Setting up a cluster of WSO2 Application servers
  3. Configure Nginx for load balancing

Let’s dive in…! You can skip any parts you are already familiar with ;)

1. Setting up Nginx server locally

Nginx is an open source software for load balancing, web serving, reverse proxying, caching, and many more. Nginx also has a enterprise version called Nginx plus with some added features like session persistence, Advanced HTTP, TCP, and UDP load balancing, live activity monitoring, etc. You can refer [1] for feature comparison between opensource Nginx and Nginx plus.

As this is an experiment you can either use Nginx plus trial or the open source Nginx. Nginx trial can be requested via Nginx website[2]. Or you can use apt-get to install the open source Nginx.

sudo apt-get install nginx

After installing Nginx try below command to verify.

nginx -v

2. Setting up a cluster of WSO2 Application servers

You can skip this and go to section 3, if you will be using different servers ;)

You can use [3] to download WSO2 Application server. By default WSO2 servers start in port “9443”. But you can set a port offset in carbon.xml to run multiple instances in the same machine.

  • Set the offset in one Application server as follows in “repository/conf/carbon.xml”
  • Set hostname for the servers in “repository/conf/carbon.xml”

Add the below host entries to your “/etc/hosts” file. You have to map the hostnames with the IP address of the Load balancer machine(in our case it’s the local host 127.0.0.1).

<IP-of-LB> as.wso2.com

  • Enable clustering between the servers in “repository/conf/axis2/axis2.xml”

In the same clustering block,

Set the membership scheme to “WKA” to enable the well-known address registration method.

Set the local member port as follows. This port will be specified when identifying members. Remember to make the port unique for each server instance. e.g 4100 and 4200

Specify the members as follows. As we have two instances there will be only one member for each. Make sure to specify the port of the member server correctly.

  • Start the two Application servers.

3. Configure Nginx for load balancing

  • Generate self signed certificates for Nginx plus as follows.

Refer [4]. You can skip this if you will not be using SSL for secure communication.

  1. Create the Server Key.
    $sudo openssl genrsa -des3 -out server.key 1024
  2. Certificate Signing Request.
    $sudo openssl req -new -key server.key -out server.csr
  3. Remove the password.
    $sudo cp server.key server.key.org
    $sudo openssl rsa -in server.key.org -out server.key
  4. Sign your SSL Certificate.
    $sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

While creating keys, enter the host name (as.wso2.com) as the common name.

  • Add the self signed certficates to WSO2 servers’ client trust store.

keytool -importcert -alias nginxCert -keystore client-truststore.jks -storepass wso2carbon -file <path-to-cert>/server.crt

I have a detailed story in a previous post.

  • Create a vhost configuration file “/etc/nginx/sites-enabled/<conf-name>”

<conf-name> can be anything and I have used “as.wso2.com”

You can read more about nginx configurations in [5].

  • Include the above conf file in “/etc/nginx/nginx.conf”

include /etc/nginx/sites-enabled/*;

  • Test and start the nginx service with new configs.

Test whether configs are correct:

sudo nginx -t

The result should be as follows. If not please verify and correct the errors specified.

Restart Nginx with new configs:

sudo service nginx restart

Check [6] for a more detailed description.

Enjoy!

--

--