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
Perangkat yang digunakan di tutorial ini:
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 |
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 |
Browse domain berulang kali, akan menampilkan halaman dari Node1 dan Node2 secara bergantian.
Busi adalah komponen penting pada kendaraan yang berfungsi sebagai pemantik bunga api agar mesin dapat…
Akhirnya hadir! VMware akhirnya memungkinkan Anda memanfaatkan Azure AD sebagai Penyedia Identitas (IdP) utama. Hari…
Berikut jadwal imsak dan buka puasa Ramadhan 2025 di Kota Serang dari tanggal 1 Ramadhan -…
Jakarta - Pemerintah telah menetapkan awal Ramadan 2025 berdasarkan hasil sidang isbat oleh Kemenag. Begitu pula dengan organisasi masyarakat…
Siapa sangka, salah satu warganet justru mendapat informasi tak terduga yang berasal dari Meta AI.…
Ketika menjalani rutinitas sehari-hari tentu saja kamu pernah merasa suntuk atau jenuh. Supaya kamu bisa…