我试图使用HAProxy在后端服务器之间负载平衡,但出于某种原因,后端服务器的端口一直被插入。
示例:我连接到192.168.1.1 (通过端口80,因为我试图从chrome加载网页)。HAProxy工作,我得到了一个后端的网页
然后我尝试点击页面中的一个链接。链接地址仍然是192.168.1.1 (没有端口),但是当我单击它时,我的URL就变成192.168.1.1:8000/mypage (即使我是由192.168.1.2服务器提供服务)。
我使用wireshark查看消息,HAProxy有时发送301个响应,表示页面已永久移动到192.168.1.1:8000/mypage。对页面内容的其余GET请求转到:8000 url。此外,有时即使broswer中的链接是192.168.1.1/mypage,初始的GET请求仍然发送到:8000
是否有一种方法可以重新配置/更改HAProxy,使其不具有此行为?它的要点是,当我输入一个url时,我得到了正确的页面,而url并没有改变我输入的内容,但是当我单击一个链接(即使是同一个页面,在broswer中的链接是相同的)时,我得到了正确的页面,但是我的url更改为添加了后端端口。
我的HAProxy配置如下
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
stats timeout 30s
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main
bind *:80
stats enable
stats uri /stats
stats realm HAProxy\ Statistics
stats auth admin:password
default_backend backend_main
backend backend_main
balance roundrobin
option prefer-last-server
cookie server_cookie insert indirect nocache
server s1 192.168.1.1:8000 check cookie s1
server s2 192.168.1.2:8000 check cookie s2发布于 2020-08-10 23:47:46
真正的解决办法是更改后端服务器配置,该配置正在进行重定向。当后端发送一个Location:头(例如,当您从index.html目录中省略它时),HAProxy只会传递它。或者当您的页面HTML包含到192.168.1.1的直接链接时,那么很明显,当单击浏览器时,浏览器就会去那里。
(至少)有两种方法来解决这个问题。首先是在后端web服务器上--确保没有任何页面包含192.168.1.1,并且任何Location:头都有正确的端口(80)。第二种方法是在HAProxy中重写Location:头。
http-request replace-header Location ^http://192.168.1.1:8000/(.*)$ http://192.168.1.1/\1如果您使用HAProxy方法,请注意,您需要分别对IP地址和域名进行访问(如果您将同时使用这两种方式并希望两者都使用)。
https://serverfault.com/questions/1029591
复制相似问题