实验环境

192.168.186.129 centos7 安装nginx的负载均衡器
192.168.186.128 centos7 提供真实服务的httpd服务器
192.168.186.130 centos7 提供真实服务的httpd服务器
本地windows

关闭防火墙和selinux

systemctl stop firewalld
setenforce 0

httpd服务器配置

yum install httpd -y
#在/etc/httpd/conf/httpd.conf修改httpd端口
listen 8080
#在192.168.186.128的/var/www/html中创建a.html,向里面写入web1,在192.168.186.130的/var/www/html中创建a.html,向里面写入web2
echo web1 > a.html
echo web2 > a.html
#启动服务
systemctl start httpd

nginx负载均衡器配置

upstream myserver {
server 192.168.186.128:8080;
server 192.168.186.130:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.186.129;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
proxy_pass http://myserver/a.html;
}
}

测试

windows端浏览器输入192.168.186.129:80看是否能正常轮询切换到web1、web2

问题

如果出现返回页面报400错误,有可能是invalid hostname,也可能是其他http header导致服务器端无法正常解析。