在Debian上實現LNMP負載均衡,核心是通過Nginx配置反向代理和負載均衡策略,步驟如下:
安裝Nginx:
sudo apt update && sudo apt install nginx
sudo systemctl start nginx && sudo systemctl enable nginx
配置負載均衡規則:
編輯Nginx配置文件(如/etc/nginx/nginx.conf
或站點配置文件),定義后端服務器組并設置負載策略:
http {
upstream backend {
server 192.168.1.101 weight=3; # 權重示例
server 192.168.1.102;
server 192.168.1.103 backup; # 備份服務器
}
server {
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
支持策略:輪詢(默認)、最少連接(least_conn
)、IP哈希(ip_hash
)等。
配置后端服務:
fastcgi_pass
指向Unix socket或IP:端口)。啟用高可用與監控:
proxy_next_upstream
等指令實現故障檢測。測試與優化:
使用sudo nginx -t
檢查配置語法,sudo systemctl reload nginx
重新加載配置,通過壓力測試工具(如ab)驗證負載均衡效果。
關鍵說明:
ufw
)及安全策略(如限制IP訪問)。