0

I'm trying to use HAProxy to load balance between backend servers, but for some reason the port of the backend server keeps getting inserted.

Example: I connect to 192.168.1.1 (over port 80, since I'm trying to load a webpage from chrome). HAProxy works and I get served a webpage by one of the backends

I then try to click a link inside the page. The link address is still 192.168.1.1 (no port), but when I click it my URL then becomes 192.168.1.1:8000/mypage (even if I'm being served by the 192.168.1.2 server).

I used wireshark to look at the messages, and HAProxy sometimes sends a 301 response, saying the page has been moved permanently to 192.168.1.1:8000/mypage. The rest of the GET requests for the page's content then go to the :8000 url. Additionally, sometimes even though the link in broswer is to 192.168.1.1/mypage, the initial GET request is still sent to :8000

Is there a way to reconfigure/change HAProxy to not have this behavior? The gist of it is that when I type in a url, I get the right page and the url doesn't change from what I typed in but when I click a link (even to the same page, with the link in broswer being the same), I get the right page BUT my url changes to have the backend port added to it.

My HAProxy config is below

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
forkwasher
  • 75
  • 1
  • 4

1 Answers1

0

The real answer is to change the backend server configuration, which is doing the redirection. When the backend sends a Location: header (e.g. when you omit index.html from a directory in the URL), HAProxy will simply pass it through. Or when your page HTML includes a direct link to 192.168.1.1, then obviously that is where the browser will go when clicked.

There's (at least) two ways to fix this. First is on the backend web server - make sure no pages contain 192.168.1.1 and any Location: headers have the right port (80). Second approach is in HAProxy by rewriting the Location: header, e.g.

http-request replace-header Location ^http://192.168.1.1:8000/(.*)$ http://192.168.1.1/\1

If you use the HAProxy approach, note that you would need to do it for IP addresses and domain names separately (if you will be accessing using both and want both to work).

tater
  • 1,445
  • 2
  • 10
  • 12