Load Balancing adalah sebuah mekanisme untuk membagi atau mendistribusikan trafik ke beberapa server. Nginx selain berfungsi sebagai web server bisa juga berfungsi sebagai load balancer.
Metode Load Balancing
- Round Robin: mendistribusikan trafik ke setiap server secara bergantian.
- Least Connections: mendistribusikan trafik ke server yang paling sedikit koneksi aktifnya.
- IP Hash: mendistribusikan trafik ke server yang sama ketika visitor pertama kali melakukan request.

0.Perangkat yang digunakan
Perangkat yang digunakan di tutorial ini:
- 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 dan PHP-FPM di Node
Install Nginx di Node1 dan Node2.
1 2 3 | sudo apt install nginx php php–fpm –y sudo systemctl status nginx sudo systemctl status php7.2–fpm |
Membuat file index.php di Node1.
1 2 | sudp mkdir /var/www/defnex.com sudo echo “<h1>node1</h1>” > /var/www/defnex.com/index.php |
Membuat file index.php di Node2.
1 2 | sudo mkdir /var/www/defnex.com sudo echo “<h1>node2</h1>” > /var/www/defnex.com/index.php |
Membuat server block di Node1 dan Node2.
1 | sudo nano /etc/nginx/conf.d/defnex.com.conf |
File konfigurasi 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; } |
Uji dan restart Nginx.
1 2 3 | sudo nginx –t sudo systemctl restart nginx sudo systemctl status nginx |
2.Install dan Konfigurasi Nginx di Load Balancer
Install Nginx.
1 2 | sudo apt install nginx –y sudo systemctl status nginx |
Membuat server block untuk load balancing domain defnex.com.
1 | sudo nano /etc/nginx/conf.d/lb–defnex.com.conf |
File konfigurasi server block load balancing domain defnex.com.
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; } } |
Pada konfigurasi upstream backend dituliskan IP address dari backend server. Secara default metode load balancing yang digunakan adalah Round Robin.
Konfigurasi load balancing jika menggunakan metode Least Connections.
1 2 3 4 5 | upstream backend { least_conn; server 10.130.127.167; #node1 server 10.130.128.35; #node2 } |
Konfigurasi load balancing jika menggunakan metode IP Hash.
1 2 3 4 5 | upstream backend { ip_hash; server 10.130.127.167; #node1 server 10.130.128.35; #node2 } |
Uji dan restart Nginx.
1 2 3 | sudo nginx –t sudo systemctl restart nginx sudo systemctl status nginx |
3.Pengujian
Browse domain berulang kali, akan menampilkan halaman dari Node1 dan Node2 secara bergantian.

