haproxy use_backend condition (acl) is missing the backend - haproxy

I have an application which I installed in two servers for balancing issue.
I wanted to route the traffic like below:
server 1: client
server 2: admin
server 1 app config:
host: 192.168.0.101
port: 5031
server 2 app config:
host: 192.168.0.102
port: 5032
Application is same in both servers with same database connection.
I installed haproxy in 192.168.0.101 with port 5033 and setup:
frontend hh-test
bind 192.168.0.101:5033
mode tcp
option tcplog
acl is_admin path_reg ^/admin/sales$
use_backend server2 if is_admin
default_backend server1
backend server2
mode tcp
server admin 192.168.0.102:5032
backend server1
mode tcp
server client 192.168.0.101:5031
It does work. When I access http://192.168.0.101:5033 It serves from Server 1 and when I access http://192.168.0.101:5033/admin/sales it serves from server2 ...as expected. But some request goes to server1 accessing same /admin/sales url. Very few. I am not sure what I am doing wrong that some request goes to the other backend. Can I have an insight please?

I had to use http mode to catch url properly.
So working version is:
frontend hh-test
bind 192.168.0.101:5033
mode http
option httplog
acl is_admin path_reg ^/admin/sales$
use_backend server2 if is_admin
default_backend server1
backend server2
mode http
server admin 192.168.0.102:5032
backend server1
mode http
server client 192.168.0.101:5031

Related

HAProxy not redirecting http to https (ssl)

I'm using HAProxy for load balancing and only want my site to support https. Thus, I'd like to redirect all requests on port 80 to port 443.
How would I do this?
Edit: We'd like to redirect to the same url on https, preserving query params. Thus, http://foo.com/bar would redirect to https://foo.com/bar
frontend httpfront
mode http
bind *:80
redirect scheme https code 301 if !{ ssl_fc }
You need configure frontend for 443 port.
frontend (port 80) -> frontend (port 443) -> backend
Check my example:
frontend httpfront
mode http
bind *:80
redirect scheme https code 301 if !{ ssl_fc }
frontend httpsfront
mode tcp
bind *:443
default_backend app
backend app
mode tcp
balance roundrobin
server server01 10.10.10.11:443 check
server server02 10.10.10.12:443 check

HAProxy - Partial SSL Offloading

Currently, I'm using tcp mode to redirect SSL traffic to two backend nodes where the certs are loaded and processed. This is working as expecting, and in apache with SNI I can process the necessary domains.
For one of these domains, I have a need to move it to a different backend and server. My question - is it possible to get this setup in haproxy within the same frontend ip / port? I do not want to load ALL the certs into HAProxy - but it would be fine to load just the cert for the site that needs to point at a different backend.
Basically, can I run something like this without needed to load all ssl certs into haproxy?
acl host_mysite hdr(host) -i mysite.example.com
Update. Here are the existing config sections:
frontend http_frontend
bind :::80 v4v6
tcp-request inspect-delay 5s
default_backend http_backend
acl host_mysite hdr(host) -i mysite.example.com
use_backend http_backend2 if host_mysite
backend http_backend
balance roundrobin
mode http
default-server inter 4s rise 3 fall 2
fullconn 20000
reqadd X-Forwarded-Proto:\ https if { ssl_fc }
server server1 192.168.192.1:80 maxconn 10000 check
server server2 192.168.192.2:80 maxconn 10000 check
backend http_backend2
mode http
default-server inter 4s rise 3 fall 2
fullconn 20000
server server3 192.168.192.3:80 maxconn 10000 check
frontend ssl_frontend
bind :::443 v4v6
mode tcp
default_backend ssl_backend
backend ssl_backend
balance roundrobin
mode tcp
default-server inter 4s rise 3 fall 2
fullconn 20000
reqadd X-Forwarded-Proto:\ https if { ssl_fc }
option ssl-hello-chk
server server1 192.168.192.1:443 maxconn 10000 check
server server2 192.168.192.2:443 maxconn 10000 check

haproxy: share port between HTTPS and XMPP

Followed the myriads of tutorials and still no go. I don't need haproxy to handle SSL as I'm passing https directly to the nginx SSL port and XMPP also has its own encryption.
Here is what I have (MY.DOMAIN is valid for the nginx SSL cert). Everything always falls through to XMPP. I'm also open to reversing the approach - does anyone have a reliable way to detect XMPP (with SSL)?
global
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 120s
timeout queue 1m
timeout connect 10s
timeout client 1440m # Modified
timeout server 1440m # Modified
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main
mode tcp
bind :443
tcp-request inspect-delay 5s
#Neither of these work:
#use_backend www-ssl if { req.ssl_hello_type 1 }
use_backend www-ssl if { req.ssl_sni -i MY.DOMAIN }
default_backend xmpp
option tcplog
log global
backend xmpp
mode tcp
option tcplog
server app1 127.0.0.1:5222
backend www-ssl
mode tcp
server www-ssl 127.0.0.1:1443
The above configuration actually is working. I had some redirect on the backend that confused me.
I'm not 100% sure that it's working all the time (for example, curl is getting XMPP and I'm not sure what's so different about curl's SSL), but at least for Chrome requests the HTTPS detection is working.
UPDATE: adding this to frontend makes it work for curl and browsers:
acl content_present req_len gt 0
tcp-request content accept if content_present

HAproxy web server redirects

I have only one PUBLIC IP address and two web servers one for each site I run. I have been trying to setup an HAproxy to redirect the requests to the correct web server, as following:
If user calls www.domain1.com, the HA proxy send this request to internal IP 192.168.0.249
If user calls www.domain2.com, it will be redirect to internal IP 192.168.0.100
I have made some searches on the Internet and I have made the following configuration for HAProxy.
global
log 127.0.0.1 local0 notice
maxconn 2000
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
timeout connect 5000
timeout client 10000
timeout server 10000
#stats
listen StatServer 0.0.0.0:80
mode http
stats enable
stats usi /haproxy?stats
stats reaml Strictly\ Private
stats auth user:pass
frontend http-in
bind *:80
default_backend domain1
reqadd X-forwarded-Proto:\ http
acl req_dimain2 hdr_beg(host) -i www.domain2.com
acl req_domain2 hdr_beg(host) -i domain2.com
use_backend domain2 if req_domain2
backend domain2
mode http
balance roundrobin
option httplog
option httpclose
option forwardfor
cookie JSSESIONID prefix
option httpchk HEAD /check.txt HTTP/1.0
server domain2 192.168.0.249 cookie avi_cr weight 1 maxconn 2000 check port 80 inter 2000 rise 2 fall 2
backend dimain1
mode http
balance roundrobin
option httplog
option httpclose
option forwardfor
cookie JESSESSIONID prefix
option httpchk HEAD /check.txt HTTP/1.0
server domain1 192.168.0.100 cookie atc_srv weight 1 maxconn 2000 check port 80 inter 2000 rise 2 fall 2
On haproxy stats page I always have both domain1 and domain2 DOWN and pages got error 503. Any help will be appreciated.
Regards.
Marcio
503 Error means HAproxy server didn't get proper web server.You can check on HAproxy front-end part.

haproxy acl not working in https/tcp mode

I am experiencing some problems, it seems I can't get acl's to work in tcp mode, everything works in http mode.
Here is my config.
frontend http *:80
acl http_test_acl path_beg -i /test
use_backend http_test if http_test_acl
default_backend http_default
backend http_test
balance roundrobin
server httptest 10.10.10.10:80 check
backend http_default
balance roundrobin
server httpdefault 10.10.10.10:80 check
############# HTTPS #################
frontend https *:443
mode tcp
acl https_test_acl path_beg -i /test
use_backend https_test if https_test_acl
default_backend https_default
backend https_test
mode tcp
balance roundrobin
server httpstest 10.10.10.10:443 check
backend https_default
mode tcp
balance roundrobin
server httpsdefault 10.10.10.10:443 check
Don't pay attention to ip 10.10.10.10 as I have hidden my orginal one. Could you please let me know why https is not working, http frontend/backend acl rules are working just fine.
cheers
Cause your https servers are in tcp mode (as they should be for ssl), so a layer 7 rule wont work.
for acl to work, disable tcp mode then set up ssl on the servers on your backend(hence the ssl keyword)
frontend https *:443
acl https_test_acl path_beg -i /test
use_backend https_test if https_test_acl
default_backend https_default
backend https_test
balance roundrobin
server httpstest 10.10.10.10:443 ssl check
backend https_default
balance roundrobin
server httpsdefault 10.10.10.10:443 ssl check
Alternatively instead of having to setup ssl on both your backend servers; use private IPS in the backend servers and make sure ports on the backend servers arent open to the world
backend https_test
balance roundrobin
server httpstest some_private_ip:8000 check