set-header for HAProxy responses

New in version 1.5 is the http-response add-header directive. As written in the documentation -- this can be used in frontend, backend and listen.

What I was trying to within a frontend section:

use_backend some_backend                if some_condition
http-response add-header Vary Origin    if some_condition

But this has no effect. The same line placed in the backend section works perfectly fine. Can someone please help me understand, what I am missing here? I don't understand, why this does not work in the frontend section.

My guess would be that within the fronted there is no response yet and so the directive fails. I tried using it before and after die use_backend directive.


Solution 1:

Running haproxy in foreground (haproxy -d -V -f haproxy.cfg) with your sample displayed the following warning:

acl 'some_condition' will never match because it only involves keywords that are incompatible with 'frontend http-response header rule'

Fiddling with my config, I found that this warning is displayed when using this ACL:

acl some_condition req.hdr(Content-Type) -i application/json

But not when using this ACL:

acl some_condition res.hdr(Content-Type) -i application/json

The difference being res vs. req, so checking response headers instead of request headers. This leads me to believe that a http-response header rule in a frontend is incapable of inspecting request headers.

I don't know if this is intended functionality by HAProxy, but it certainly hasn't been clearly documented.