kamleshchandnani / haproxy

This repository consists of small small snippets to better understand haproxy configuration
4 stars 1 forks source link

HAPROXY multiple context based routing not working #1

Open sathishece170 opened 5 years ago

sathishece170 commented 5 years ago

Im having below haproxy configuration to route two urls based on context path. But during my testing found that app1 is always working fine and app2 is not working but routed to app1 itself.

Using reqirep to ignore the context path while routing to application (this is needed by my application)

From browser i will call like,

http://test.com/app1 should be routed to app1 http://test.com/app2 should be routed to app2

Any help would be appreciated?

frontend http_frontend
    bind *:80
    mode http
http-response set-header Strict-Transport-Security max-age=15768000
reqirep  ^([^\ :]*)\ /app1(.*) \1\ /\2
reqirep  ^([^\ :]*)\ /app2(.*) \1\ /\2
###################### ACL ###########################
 acl acl_app1 hdr_dom(host) test.com path_beg -i /app1
  use_backend acl-backend_app1 if acl_app1
###################### ACL ###########################
 acl acl_app2 hdr_dom(host) test.com path_beg -i /app2
  use_backend acl-backend_app2 if acl_app2
###################### ACL ###########################'
kamleshchandnani commented 5 years ago

There are few thing you're confusing them.

  1. reqirep won't redirect your path so your acl check will fail.
  2. your acl check for /app1 will only pass if your request path contains /app1 and not reqirep.

if from your browser you are directly making a call http://test.com/app1 then you don't even need reqirep

frontend http_frontend
    bind *:80
    mode http
http-response set-header Strict-Transport-Security max-age=15768000
###################### ACL ###########################
 acl acl_app1 hdr_dom(host) test.com path_beg -i /app1
  use_backend acl-backend_app1 if acl_app1
###################### ACL ###########################
 acl acl_app2 hdr_dom(host) test.com path_beg -i /app2
  use_backend acl-backend_app2 if acl_app2
###################### ACL ###########################'
sathishece170 commented 5 years ago

As per your code snippet request with /app1 works fine but /app2 doesnt since when request comes it matches the host with /app1 filter & failed. It never go and match the regex with second acl filter because of same host.

kamleshchandnani commented 5 years ago

try this

frontend http_frontend
    bind *:80
    mode http
http-response set-header Strict-Transport-Security max-age=15768000
acl is-test-app hdr_dom(host) -i test.com
###################### ACL ###########################
 acl is-app1 path_beg -i /app1
 use_backend backend_app1 if is-app1 is-test-app
###################### ACL ###########################
 acl is-app2 path_beg -i /app2
 use_backend backend_app2 if is-app2 is-test-app
sathishece170 commented 5 years ago

Thanks for responding!

I have tried this already but it is asking for username and password after adding this when i hit the url. This looks strange to me as i have not enabled security here.

Will HAProxy version matters since i'm using 1.8.1?

kamleshchandnani commented 5 years ago

Not sure but previously it wasn't working because you shouldn't put conditions in acls and because your first check was for host that's why it was going to /app1 always with https://github.com/kamleshchandnani/haproxy/issues/1#issuecomment-465859764 that would be fixed.

sathishece170 commented 5 years ago

Ok. I will check it and thanks for your help!!