บทนำ
Nginx Load Balancing เป็นเทคนิคที่ช่วยให้การกระจายโหลดไปหลาย Backend Server ทำให้เพิ่มประสิทธิภาพ (Performance) และลดความเสี่ยงในการ Downtime บทความนี้จะสอนวิธีตั้งค่า Upstream Block, Load Balancing Methods ต่างๆ เช่น Round Robin, Least Connections, IP Hash และการตรวจสอบ Health Checks
Load Balancing คืออะไร และทำไมต้องใช้
Load Balancing เป็นเทคนิคในการกระจายคำขอ (Requests) ของผู้ใช้ไปหลาย Backend Servers เพื่อให้ไม่มี Server ใดต้องรับภาระงานมากเกินไป ประโยชน์ของ Load Balancing ได้แก่:
- เพิ่มประสิทธิภาพ (Performance) ของเว็บแอปพลิเคชัน
- ลดความเสี่ยงในการหยุดชะงัก (Downtime)
- รองรับผู้ใช้จำนวนมากได้พร้อมกัน
- ใช้ทรัพยากรของ Servers ได้อย่างมีประสิทธิภาพ
เมื่อใช้ Cloud VPS จาก Dot Enterprise (https://de.co.th/cloud-vps) คุณสามารถตั้งค่า Load Balancing ได้ง่ายๆ โดยใช้ Nginx
Nginx ในฐานะ Load Balancer
Nginx สามารถทำหน้าที่เป็น Load Balancer ได้ดี มีความเสถียร ใช้ทรัพยากรน้อย และสามารถจัดการ Requests จำนวนมากได้ในเวลาเดียวกัน
Upstream Block: ประกาศ Backend Servers
ขั้นแรกให้สร้าง Upstream Block ในไฟล์ Nginx config เพื่อประกาศ Backend Servers:
upstream backend_servers {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
Round Robin: Load Balancing ตามลำดับ
Round Robin คือวิธี Default ที่ Nginx จะกระจายคำขอให้ Backend Servers ตามลำดับ ซ้ำไปซ้ำมา เว่ Backend เหล่านี้มีความสามารถเท่าเทียมกัน:
upstream backend_servers {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Least Connections: ส่งไปยัง Backend ที่มี Connection น้อยที่สุด
วิธี Least Connections จะส่ง Request ไปยัง Backend Server ที่มี Active Connections น้อยที่สุด เหมาะสำหรับการจัดการงานที่มีความยาวต่างกัน:
upstream backend_servers {
least_conn;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
IP Hash: Session Persistence
IP Hash จะตรึง Client ไปยัง Backend Server เดียวกัน โดยใช้ IP Address ของ Client เป็นพื้นฐาน ทำให้ Session ของผู้ใช้ยังคงอยู่ที่ Server เดียว:
upstream backend_servers {
ip_hash;
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
}
Weighted Load Balancing: กำหนดน้ำหนัก
หากบาง Backend Server มีความสามารถสูงกว่า สามารถกำหนด Weight เพื่อให้รับคำขอมากขึ้น:
upstream backend_servers {
server 192.168.1.10:8080 weight=5;
server 192.168.1.11:8080 weight=3;
server 192.168.1.12:8080 weight=2;
}
ในตัวอย่างนี้ Backend Server แรกจะรับคำขอ 50% Backend Server ที่สองรับ 30% และ Backend Server ที่สามรับ 20%
Health Checks: ตรวจสอบสถานะ Backend Servers
การตรวจสอบ Health ของ Backend Servers เป็นสิ่งสำคัญ เพื่อไม่ส่ง Request ไปยัง Server ที่หยุดทำงาน ใช้ max_fails และ fail_timeout:
upstream backend_servers {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;
}
max_fails – จำนวนครั้งของการล้มเหลวก่อนถือว่า Server หยุดทำงาน
fail_timeout – ระยะเวลาที่ Nginx จะไม่ส่ง Request ไปยัง Server ที่ล้มเหลว
SSL Termination ที่ Load Balancer
ให้ Load Balancer (Nginx) จัดการการเข้ารหัส SSL/TLS แล้วส่ง HTTP ไปยัง Backend Servers เพื่อลดภาระ:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
proxy_pass http://backend_servers;
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_set_header X-Forwarded-Proto $scheme;
}
}
Monitoring: ดู Nginx Status
เปิดใช้งาน stub_status เพื่อติดตามสถานะ Load Balancer:
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
แก้ไขปัญหาที่พบบ่อย
502 Bad Gateway: เกิดจากไม่สามารถเชื่อมต่อกับ Backend Server ตรวจสอบให้แน่ใจว่า Backend Server กำลังทำงาน
Backend Timeout: ใช้ proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout เพื่อเพิ่มเวลา Timeout
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
บทสรุป
การตั้งค่า Nginx Load Balancing ช่วยให้สามารถกระจายโหลดไปหลาย Backend Servers ได้อย่างมีประสิทธิภาพ บทความนี้ครอบคลุมวิธีการต่างๆ เช่น Round Robin, Least Connections, IP Hash, Weighted Load Balancing และ Health Checks เมื่อใช้ Cloud VPS จาก Dot Enterprise (https://de.co.th/cloud-vps) คุณสามารถตั้งค่า Load Balancing ได้อย่างเต็มที่เพื่อเพิ่มประสิทธิภาพและเสถียรภาพของเว็บแอปพลิเคชัน
