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