基于域名的访问:
首先apache client1与apache client2做一个相同的虚拟主机配置
例如都做成:
client1端(192.168.7.196)配置:
# mkdir /usr/local/apache2/htdocs/www.example.com/
# cd /usr/local/apache2/htdocs/www.example.com/
# echo "IP:192.168.7.196" > index.html
# vi /usr/local/apache2/conf/httpd.conf
找到461行,去掉该行注释:
----------------
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
----------------
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
删除配置文件内非注释内容,并添加:
--------------
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/www.example.com"
ServerName
</VirtualHost>
--------------
重启apache服务
# /usr/local/apache2/bin/apachectl restart
client2端(192.168.7.197)配置:
# mkdir /usr/local/apache2/htdocs/www.example.com/
# cd /usr/local/apache2/htdocs/www.example.com/
# echo "IP:192.168.7.197" > index.html
# vi /usr/local/apache2/conf/httpd.conf
找到461行,去掉该行注释:
----------------
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
----------------
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
删除配置文件内非注释内容,并添加:
--------------
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/htdocs/www.example.com"
ServerName
</VirtualHost>
--------------
重启apache服务
# /usr/local/apache2/bin/apachectl restart
最后客户端浏览器做好的解析地址是192.168.7.198
客户端访问如图:
客户端浏览器最终访问地址时会随机在client1与client2虚拟主机目录之间来回跳转,实现基于域名的负载均衡。
注:haproxy启动报错
Starting proxy : cannot bind socket
一般是haproxy server安装web服务(apache或nginx),由于默认端口为80,和haproxy配置文件设置的端口冲突导致,关闭web服务或更改端口即可。