How to Configure Load Balancing with Nginx
Load Balancing is a mechanism for sharing or distributing traffic to multiple servers. Nginx in addition to functioning as a web server can also function as a load balancer.
Load Balancing Method
- Round Robin: distributes traffic to each server in turn.
- Least Connections: distributes traffic to the server with the least active connections.
- IP Hash: distributes the same traffic source to the same server the first time you make a request.
0.Equipment used
Equipment used in this tutorial:
- OS Ubuntu 18.04 LTS
- Nginx web server
- PHP-FPM 7.2
- Node1: 10.130.127.167
- Node2: 10.130.128.35
- LoadBalancer: 128.199.187.215
- Domain: defnex.com
1.Install Nginx and PHP-FPM on Node
Install Nginx on Node1 and Node2.
1 2 3 | sudo apt install nginx php php-fpm -y sudo systemctl status nginx sudo systemctl status php7.2-fpm |
Create file index.php on Node1.
1 2 | sudp mkdir /var/www/defnex.com sudo echo "<h1>node1</h1>" > /var/www/defnex.com/index.php |
Create file index.php on Node2.
1 2 | sudo mkdir /var/www/defnex.com sudo echo "<h1>node2</h1>" > /var/www/defnex.com/index.php |
Create server block on Node1 and Node2.
1 | sudo nano /etc/nginx/conf.d/defnex.com.conf |
Configuration file for Nginx server block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | server { listen 80; server_name defnex.com www.defnex.com; root /var/www/defnex.com/; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $fastcgi_script_name =404; include fastcgi_params; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param DOCUMENT_ROOT $realpath_root; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; } access_log /var/log/nginx/defnex.com.access.log; error_log /var/log/nginx/defnex.com.error.log warn; } |
Test and restart Nginx.
1 2 3 | sudo nginx -t sudo systemctl restart nginx sudo systemctl status nginx |
2.Install and Configure Nginx on Load Balancer
Install Nginx.
1 2 | sudo apt install nginx -y sudo systemctl status nginx |
Creating a Nginx server block for the load balancing with domain defnex.com.
1 | sudo nano /etc/nginx/conf.d/lb-defnex.com.conf |
Nginx server block configuration code for load balancing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | upstream backend { server 10.130.127.167; #node1 server 10.130.128.35; #node2 } server { listen 80; server_name defnex.com www.defnex.com; location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://backend; } } |
In the upstream backend configuration the IP address of the backend server is written. By default the load balancing method used is Round Robin.
Load balancing configuration if using the Least Connections method.
1 2 3 4 5 | upstream backend { least_conn; server 10.130.127.167; #node1 server 10.130.128.35; #node2 } |
Load balancing configuration if using the IP Hash method.
1 2 3 4 5 | upstream backend { ip_hash; server 10.130.127.167; #node1 server 10.130.128.35; #node2 } |
Test and restart Nginx.
1 2 3 | sudo nginx -t sudo systemctl restart nginx sudo systemctl status nginx |
3.Testing
Browse the domain repeatedly, will display pages from Node1 and Node2 alternately.
If you found this article helpful and would like to support my work, consider making a donation through PayPal. Your support helps me continue creating useful content and tutorials. Thank you!
Donate via PayPal: https://paypal.me/musaamin
Bagaimana seting nginx dengan tanpa nama domain tetapi ip atau localhost
gak perlu setting, cukup install aja nginx, defaultnya akses pakai ip address