我正在尝试配置 Apache 负载均衡器以将请求路由到多个后端服务器,但出于某种原因,似乎始终加载主域的内容,而不管指定的子域如何。例如,加载网站 games.example.com 应该加载后端服务器上的 /var/www/example_games 内容,但它却加载了 /var/www/example(即主域)中的内容。我已通过公开后端服务器进行了测试,并且 apache 配置在后端本身上运行良好,可以正确加载每个子域,但这个负载均衡器配置似乎存在问题,其中有关子域的信息“丢失”,导致主域(example.com)的内容加载,尽管指定了 games.example.com。以下是负载均衡器配置:
<IfModule mod_ssl.c>
# SSL certificate configurations
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
<Proxy balancer://mycluster>
BalancerMember http://172.16.111.10 loadfactor=1
# BalancerMember http://172.16.111.11 loadfactor=10
# BalancerMember http://172.16.111.12 loadfactor=10
# BalancerMember http://172.16.111.13 loadfactor=10
# Add more BalancerMembers if you have additional backend servers
# BalancerMember http://backend-server3-ip:backend-server3-port route=server3
# ...
ProxySet lbmethod=byrequests
#ProxySet stickysession=ROUTEID
#ProxySet nofailover=On
</Proxy>
#Main domain
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@localhost
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
<Location "/balancer-manager">
SetHandler balancer-manager
Require host localhost
</Location>
<Location "/server-status">
SetHandler server-status
Require host localhost
</Location>
</VirtualHost>
#GAMES DOMAIN
<VirtualHost *:443>
ServerName games.example.com
Options -Indexes
RemoteIPHeader X-Forwarded-For
ProxyPreserveHost On
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
<Location "/balancer-manager">
SetHandler balancer-manager
Require host localhost
</Location>
<Location "/server-status">
SetHandler server-status
Require host localhost
</Location>
</VirtualHost>
#CDN DOMAIN
<VirtualHost *:443>
ServerName cdn.example.com
Options -Indexes
RemoteIPHeader X-Forwarded-For
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
<Location "/balancer-manager">
SetHandler balancer-manager
Require host localhost
</Location>
<Location "/server-status">
SetHandler server-status
Require host localhost
</Location>
</VirtualHost>
</IfModule>
答案1
我找到了问题所在,ProxyPreserveHost On
每个块都需要该指令才能将请求标头转发到后端服务器。希望它能帮助其他遇到类似问题的人。