Need some help with config - use_backend with path_sub and method type

Looking to have some of the http incoming redirected to a specific backend based on path content and method type.

If path contains “string1” and is a POST → backend2
If path contains “string1” and is a PUT → backend2
If path contains “string1” and is a DELETE → backend2

default is backend1

my attempt (not working) from my cfg file:

use_backend backend2 if { path_sub /string1/ } { method POST }
use_backend backend2 if { path_sub /string1/ } { method PUT }
use_backend backend2 if { path_sub /string1/ } { method DELETE }

This was based on another use_backend instance that is working:

use_backend backend3 if { path_sub /string2/ } { path_sub /string3/ }

I’m guessing my method spec isnt correct…

Can anyone point me in the right direction?

Thanks!

Works like it is supposed to work for me. Maybe you want to elaborate, providing the full configuration and the output of haproxy -vv ?

global
 log 127.0.0.1 local0

defaults
 mode http
 timeout connect 5000
 timeout client  50000
 timeout server  50000

frontend http_in
 bind :80
 use_backend bk_one if { path_sub /string1/ } { method POST }
 use_backend bk_two if { path_sub /string1/ } { method PUT }
 use_backend bk_three if { path_sub /string1/ } { method DELETE }
 default_backend bk_default

backend bk_one
 http-request return content-type text/plain string bk_one

backend bk_two
 http-request return content-type text/plain string bk_two

backend bk_three
 http-request return content-type text/plain string bk_three

backend bk_default
 http-request return content-type text/plain string bk_default
$ curl dev/string1/
bk_default
$ curl dev/string1/ -d asd -X POST
bk_one
$ curl dev/string1/ -d asd -X PUT
bk_two
$ curl dev/string1/ -d asd -X DELETE
bk_three
$
1 Like

oh man thanks a TON for this example… I was just setting up a bunch of http servers to test my config :laughing:
I’m going to simplify my cfg for now and vet it piece by piece…

thanks

EDIT: it looks like the version of HAProxy I am using is 1.8.27 while the latest version is 2.6… not sure if that may explain my issue. but I got errors when trying to add these backends…

1 Like

Yeah, 1.8 needs a little convincing and here I used the same backend server to avoid a 503 error, marking the backend in the headers.

lukas@dev:~/haproxy-1.8$ ./haproxy -v
HA-Proxy version 1.8.27 2020/11/06
Copyright 2000-2020 Willy Tarreau <willy@haproxy.org>

lukas@dev:~/haproxy-1.8$ cat ../cert/use-backend-test.cfg
global
 log 127.0.0.1 local0

defaults
 mode http
 timeout connect 5000
 timeout client  50000
 timeout server  50000

frontend http_in
 bind :80
 use_backend bk_one if { path_sub /string1/ } { method POST }
 use_backend bk_two if { path_sub /string1/ } { method PUT }
 use_backend bk_three if { path_sub /string1/ } { method DELETE }
 default_backend bk_default

backend bk_one
 server s www.lan:80
 http-response set-status 410 reason bk_one

backend bk_two
 server s www.lan:80
 http-response set-status 411 reason bk_two

backend bk_three
 server s www.lan:80
 http-response set-status 412 reason bk_three

backend bk_default
 server s www.lan:80
 http-response set-status 419 reason bk_default


lukas@dev:~/haproxy-1.8$


lukas@dev:~$ curl dev/string1/ -v 2>&1 | grep HTTP
> GET /string1/ HTTP/1.1
< HTTP/1.1 419 bk_default
lukas@dev:~$ curl dev/string1/ -vd asd -X POST 2>&1 | grep HTTP
> POST /string1/ HTTP/1.1
< HTTP/1.1 410 bk_one
lukas@dev:~$ curl dev/string1/ -vd asd -X PUT 2>&1 | grep HTTP
> PUT /string1/ HTTP/1.1
< HTTP/1.1 411 bk_two
lukas@dev:~$ curl dev/string1/ -vd asd -X DELETE 2>&1 | grep HTTP
> DELETE /string1/ HTTP/1.1
< HTTP/1.1 412 bk_three
lukas@dev:~$
1 Like

Thanks a ton! these two test methods have already saved me hours.

I may have found my issue, and it’s annoyingly simple… specifying path_sub /string1/ instead of path_sub /string1 it wasn’t redirecting queries missing the trailing slash (oops).

Will implement these changes in the original setup and hopefully I’m good here.

Also planning on looking into the latest stable release for upgrade…

Thanks again!

1 Like