centos 7.6部署haproxy动静分离
准备环境:
client:192.168.127.141
haproxy:192.168.127.142
web1:192.168.127.143
web2:192.168.127.144
php1:192.168.127.130
php2:192.168.127.131
----------------------------------------------------------------------------------------
web1 && web2
yum -y install httpd && systemctl start httpd
web1测试页面
echo web1 > /var/www/html/index.html
web2测试页面
echo web2 > /var/www/html/index.html
php1 && php2
yum -y install php php-fpm httpd && systemctl start php-fpm httpd
php1测试页面
echo php1 > /var/www/html/index.php
php2测试页面
echo php2 > /var/www/html/index.php
------------------------------------------------------------------------------------------
haproxy负载均衡器
yum -y install haproxy
vim /etc/haproxy/haproxy.cfg
global ##全局配置
log 127.0.0.1 local3 info
maxconn 4096
user nobody
group nobody
daemon
nbproc 1
defaults
log global
mode http
maxconn 2048
retries 3
option redispatch
stats uri /haproxy
stats auth admin:1
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
bind 0.0.0.0:80
mode http
log global
option httplog
option httpclose
acl php url_reg -i \.php$ ##设置匹配php规则
acl html url_reg -i \.html$ ##设置匹配html规则
use_backend php-server if php
use_backend html-server if html
default_backend html-server ##默认访问页面
backend php-server ##设置php后端服务器
mode http
balance roundrobin ##轮询模式
option httpchk GET /index.php
cookie SERVERID insert indirect nocache
server php-A 192.168.127.130:80 weight 1 cookie 1 check inter 2000 rise 2 fall 5
server php-B 192.168.127.131:80 weight 1 cookie 2 check inter 2000 rise 2 fall 5
backend html-server ##设置html后端服务器
mode http
balance roundrobin ##轮询模式
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A 192.168.127.143:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B 192.168.127.144:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
systemctl restart haproxy && systemctl enable haproxy ##启动服务并设置开机自启
systemctl status haproxy ##查看服务状态
-----------------------------------------------------------------------------------------
client客户端访问haproxy
elinks --dump http://192.168.127.142
web1
elinks --dump http://192.168.127.142/index.html
web1
elinks --dump http://192.168.127.142/index.php
php1
QQ:1466434680
欢迎加好友进行讨论,一起学习。