Setup NGINX Load Balancer with SSL on CentOS 6

by | 9 Sep 2015 | CentOS, Linux, NGINX, OS, Server, Technology | 0 comments

Load balancing is a very common mechanism used to distribute traffic for systems. This tutorial goes through step by step from installation of NGINX on your CentOS web server and configuring your NGINX load balancer after it is installed successfully.

1. Add NGINX Repository

sudo yum install epel-release

2. Install NGINX

sudo yum install nginx

3. Start NGINX

sudo service start nginx

4. Configure NGINX config files

Locate NGINX configuration files at /etc/nginx/conf.d/ directory.

Ensure that you have SSL configured for your web server, if not you can follow the tutorial on Setting Up an SSL Secured Webserver with CentOS.

After you are done with the above steps, proceed to edit the following NGINX configuration file for SSL.

sudo nano /etc/nginx/conf.d/ssl.conf

Editing NGINX config file

Add the following above your server {} module

upstream backend {
    server backend1.mervcodes.com;
    server backend2.mervcodes.com;
    server backend3.mervcodes.com;
}

Note that the servers will be served in round robin order. You can refer to NGINX documentation for more configurations.

The next few changes is to be done within the server {} module

Enable server to listen to port 443

listen	 443;

Add server name

server_name loadbalancer.mervcodes.com

If you have followed my tutorial on Setting Up an SSL Secured Webserver with CentOS, then you can configure your SSL certificate and key as follows.

ssl                  on;
ssl_certificate	     /etc/pki/tls/certs/ca.crt;
ssl_certificate_key  /etc/pki/tls/private/ca.key;

Next under location / {} module, add the following lines.

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_pass        https://backend;
proxy_set_header  Authorization    "<YOUR BASIC AUTH IF ANY>";
proxy_set_header  Content-Type     "<YOUR CONTENT TYPE>";

After which, save your NGINX configuration file.

Then restart NGINX.

sudo service nginx restart

That’s it you are done. If you have any other questions, do feel free to drop a comment below.

0 Comments

Submit a Comment

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

Related Posts

Amazon Web Services (AWS)ApacheCentOSDatabaseLinuxMySQLOSProgrammingServerTechnology
8 Simple Steps to Setup a Web Server with Apache Virtual Hosts & MySQL in Centos

8 Simple Steps to Setup a Web Server with Apache Virtual Hosts & MySQL in Centos

This tutorial will be guiding you in setting up a web server with pre-installed Centos. In summary, you will be installing Apache and MySQL, and learning how to configure virtual hosts in Apache. Note: before proceeding with this tutorial. Make sure you figure out...

AppleiOSMobile PhonesObjective-CProgrammingSwiftTechnology
How to install Cocoapods for Objective-C/Swift development on Mac OS?

How to install Cocoapods for Objective-C/Swift development on Mac OS?

Steps to install Cocoapods Cocoapods is used in Xcode IDE for development from iOS apps using Objective-C/Swift. Cocoapods helps developers add external libraries to their iOS project easily. The following tutorial guides you on the steps to install Cocoapods on...

AndroidAndroidAppleiOSJavaMobile PhonesObjective-CProgramming
iPhone Camera
Complete overview of the mobile app development process (Infographic)

Complete overview of the mobile app development process (Infographic)

Clients always have the misperception that developing a mobile app is as easy as developing a mobile application would be a walk in a park without much effort involved. The following infographic pretty much sums up the total cost and effort for mobile app...