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

APCCentOSFedoraLinuxOSPHPProgrammingServerTechnology
Install APC (Alternative PHP Cache) in CentOS 5/6/7 and Fedora 20/21

Install APC (Alternative PHP Cache) in CentOS 5/6/7 and Fedora 20/21

APC (Alternative PHP Cache) is a free and open source tool to cache PHP codes. 1. Install Dependency Packages for APC yum install php-pear php-devel httpd-devel pcre-devel gcc make -y 2. Install APC using PECL (PHP Extension Community Library) pecl install apc...