NGINX High Availability Load Balance Apache Backend (CentOS 7.x)

Introduction

The need for load balancing and high availability is necessary to more businesses now than it ever was. It won't fix *ALL* of your problems, but it certainly doesn't hurt especially if the website is helping you bring in paychecks by selling products and/or services. The article should explain how to setup nginx with load balancing and/or high availability.

Using Nginx as a front-end HA/Load Balancing application is one of the best things you can do to keep your website accessible to the public. I prefer using Apache in the back-end because well, I like Apache and that's my preference. Either way, you can still have an nginx front-end and use an nginx back-end if you want; the front-end listeners just need to be pointing to the HTTP/HTTPS port(s) that the site(s) are hosted on.

Front-End: Install Nginx and Configure

The configuration is pretty straight forward for nginx and Apache.

  1. Install nginx
    sudo yum install -y nginx
  2. Enable daemon start on reboot
    sudo systemctl enable nginx.service
  3. Example /etc/nginx/nginx.conf you can use as a template:
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
            upstream <IDENTIFIER> {
                    least_conn;
                    server <WEB01_IP>:<WEB01_PORT>;
                    server <WEB02_IP>:<WEB02_PORT>;
            }
        server {
            listen       80;
            server_name lbext.pax8.internal;
            location / {
                    proxy_pass http://<IDENTIFIER>;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                root   html;
                index  index.html index.htm;
            }
            }
        }

    Note: There are a few changes to make to the above template configuration for nginx.

    • <IDENTIFIER> is nothing more than a canonical name and acts more as a variable. As an example, domain.com <IDENTIFIER> can be "domaincom".
    • WEB01_IP = IP Address of a web server.
    • WEB02_IP = IP Address of another web server.
    • WEB01_PORT = Port that the webserver is listening on (same goes for WEB02_PORT).

  4. Start (or restart) nginx:
    sudo systemctl restart nginx.service

Apache Back-end

  1. Install Apache (if it's not already installed):
    sudo yum install httpd -y
  2. Enable run at boot time:
    sudo systemctl enable httpd.service
  3. Configure Apache as needed

The Apache2 back-end is pretty straight forward as well and only needs a small virtualhost configured assuming you're only hosting a single website with nothing additional needed to the configuration. Here's the stripped down "domain.com.conf" in /etc/httpd/conf.d/.conf:

# Configuration for external web
<VirtualHost *:*>
        ServerName domain.com
        DocumentRoot /var/www/html
        <Directory /var/www/html>
                AllowOverride All
        </Directory>
</VirtualHost>

After all this, you should be good to go.

Notes: Back in the configuration for nginx, you'll see the following line in the "upstream" configuration section: least_conn; This tells nginx to send a client to the server with the (apparent) least number of connections. This is a normal load balancing technique to send clients to the web server that *should* be under the least load and avoid those that would already have more clients connected to them.

There are other options for this such as adjusting the weight of each web node so that they are load balanced based on priority and so forth. You can read about these configurations on the nginx website/page located here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *