在CentOS Minimal上实现负载均衡可以通过多种方法,以下是使用Nginx和HAProxy两种常见工具的配置示例。
首先,安装Nginx。由于Nginx软件包在CentOS系统默认仓库里面没有,需要安装epel仓库:
sudo dnf install epel-release -y
sudo dnf install nginx -y
允许访问Nginx的HTTP和HTTPS服务:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
编辑Nginx配置文件,通常位于 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/default.conf
。在 http
块中添加以下内容:
upstream backend {
server 192.168.1.41:80;
server 192.168.1.42:80;
}
server {
listen 80;
server_name yourdomain.com;
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;
}
}
保存配置文件并退出编辑器,然后测试配置文件是否正确:
sudo nginx -t
如果没有错误,重新加载Nginx服务:
sudo systemctl restart nginx
使用yum安装HAProxy:
sudo yum install haproxy -y
编辑HAProxy配置文件,通常位于 /etc/haproxy/haproxy.cfg
。添加以下内容:
global
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
bind *:80
default_backend servers
backend servers
balance roundrobin
server server1 192.168.1.2:80 check
server server2 192.168.1.3:80 check
保存并退出文件后,启动HAProxy服务:
sudo systemctl start haproxy
sudo systemctl enable haproxy
通过以上步骤,你可以在CentOS Minimal上实现基本的负载均衡。根据具体需求,可以选择不同的负载均衡策略和配置方法。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>