Linux LAMP 负载均衡实现指南
一、方案总览与选型
二、方案一 Nginx 或 HAProxy 反向代理
sudo yum install -y epel-release && sudo yum install -y nginxsudo yum install -y haproxyhttp {
upstream backend {
server 192.168.1.101:80;
server 192.168.1.102:80;
# 可按需添加权重、备份等:server 192.168.1.103:80 weight=2 backup;
}
server {
listen 80;
location / {
proxy_pass http://backend;
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;
}
}
}
global
log /dev/log local0
log /dev/log local1 notice
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
server s1 192.168.1.101:80 check
server s2 192.168.1.102:80 check
sudo systemctl enable --now nginx && sudo nginx -t && sudo systemctl reload nginxsudo systemctl enable --now haproxy && sudo haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl reload haproxy<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
三、方案二 LVS IP 负载均衡 + Keepalived 高可用
ipvsadm -Ln 查看调度表与后端状态四、共享内容与数据库层设计
/etc/fstab 挂载到 /var/www/html五、健康检查、监控与优化
option httpchk 或 http-check expect;后端配置 checkHTTP_GET/SSL_GET 与脚本探针,自动摘除异常 RS