I'd like to have haproxy return a 30x status code if the evaluation of a variable returns true. I see you can construct conditionals in haproxy, and evaluate things like cookies:
use_backend i if !cookie_set
but in my case variable for this conditional to eval is from a source in my environment. I can expose this variable in any method needed for haproxy to access it, and can be a simple boolean (so 'return 302 if x is true'). Is there a way to configure haproxy to support this?
I discovered a way to solve this. There are conditionals for ACLs called 'always_true' and 'always_false' which as their name suggests will cause the ACL they are used in to always return true or respectively false. So I set that in my config like so:
acl do_redir always_true
then I construct a redirect stmt using this ACL later on in my config:
redirect code 307 prefix http://someurl if do_redir
The ACL do_redir then is my mechanism for toggling the redirect behavior of haproxy. I'm changing this in the haproxy config and restarting the process using chef so it all happens pretty quick.
An alternative method for modifiyng this ACL which I haven't gotten to work yet is to use the haproxy socket. This appears to have an advantage over my current method in that it doesn't require a restart and potentially lost connections, or the complexity and added risk of modifying the haproxy config file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With