Welcome! Log In Create A New Profile

Advanced

redirect vs. logging Location hdr

Posted by Jim Freeman 
Jim Freeman
redirect vs. logging Location hdr
June 28, 2018 12:20AM
With a configuration having many 'redirect's (and wanting to know which
'Location' a given request was redirected to), I configured

capture response header Location len 64
log-format %hs

, but see no log entries for redirects I know haproxy is generating.

With further testing, I know that Location responses from downstream are
being logged - just not those generated on-host via 'redirect'.

I've scoured the docs for insight/reasoning re: this behavior, but can't
find anything.
Assuming this is as-designed, I'd appreciate any further illumination ...
Is there a way to log Location from haproxy redirects ?

Thanks!
....jfree
Willy Tarreau
Re: redirect vs. logging Location hdr
June 28, 2018 06:50AM
On Wed, Jun 27, 2018 at 04:08:27PM -0600, Jim Freeman wrote:
> With a configuration having many 'redirect's (and wanting to know which
> 'Location' a given request was redirected to), I configured
>
> capture response header Location len 64
> log-format %hs
>
> , but see no log entries for redirects I know haproxy is generating.

captures work for input data, not generated data.

> With further testing, I know that Location responses from downstream are
> being logged - just not those generated on-host via 'redirect'.

exactly.

> I've scoured the docs for insight/reasoning re: this behavior, but can't
> find anything.
> Assuming this is as-designed, I'd appreciate any further illumination ...
> Is there a way to log Location from haproxy redirects ?

There could be a solution I guess, involving a variable, though it would
require you to either double the number of redirect rules, or add a
condition to each rule. The principle would be the following :

http-request set-var(txn.location) string(/path/to/location) if ! { var(txn.location) -m found } { your condition here }
... many other such rules ...
http-request redirect location %[var(txn.location)] if { var(txn.location) -m found }

Then you can add a field for var(txn.location) in your log-format.

The only thing is that it only supports sample expressions so you don't have
the flexibility you currently have with log-format in the redirect header.
You should be able to achieve the same by using the concat() converter in
the expression but I agree that it's less readable.

Note that there is a "set-var()" converter so if your redirect expressions
are simple, you could as well set the variable on the fly. It will just not
take the result of the log-format. Example :

http-request redirect location %[string("/path/to/location"),set-var(txn.location)] if { your condition here }

It should be able to always replace my other example above and be more
efficient. But the example below won't do what you expect :

http-request redirect location /path/to/location/?src=%[src,set-var(txn.location)] if { your condition here }

as it would only capture the output of the "src" fetch function instead of
the whole line. Maybe it could be useful to support an extra "redirect"
option to duplicate the output into a variable. I don't think it would
be very complicated to have this :

http-request redirect location /path/to/foo code 302 set-var(txn.location)

But I could be wrong as I remember that variable allocation at config parsing
time is tricky.

Regards,
Willy
Jim Freeman
Re: redirect vs. logging Location hdr
June 28, 2018 07:10AM
Much thanks for the insights, clarifications, and solution possibilities.

Will chew on this for a bit ...

On Wed, Jun 27, 2018 at 10:40 PM, Willy Tarreau <[email protected]> wrote:

> On Wed, Jun 27, 2018 at 04:08:27PM -0600, Jim Freeman wrote:
> > With a configuration having many 'redirect's (and wanting to know which
> > 'Location' a given request was redirected to), I configured
> >
> > capture response header Location len 64
> > log-format %hs
> >
> > , but see no log entries for redirects I know haproxy is generating.
>
> captures work for input data, not generated data.
>
> > With further testing, I know that Location responses from downstream are
> > being logged - just not those generated on-host via 'redirect'.
>
> exactly.
>
> > I've scoured the docs for insight/reasoning re: this behavior, but can't
> > find anything.
> > Assuming this is as-designed, I'd appreciate any further illumination ...
> > Is there a way to log Location from haproxy redirects ?
>
> There could be a solution I guess, involving a variable, though it would
> require you to either double the number of redirect rules, or add a
> condition to each rule. The principle would be the following :
>
> http-request set-var(txn.location) string(/path/to/location) if ! {
> var(txn.location) -m found } { your condition here }
> ... many other such rules ...
> http-request redirect location %[var(txn.location)] if {
> var(txn.location) -m found }
>
> Then you can add a field for var(txn.location) in your log-format.
>
> The only thing is that it only supports sample expressions so you don't
> have
> the flexibility you currently have with log-format in the redirect header.
> You should be able to achieve the same by using the concat() converter in
> the expression but I agree that it's less readable.
>
> Note that there is a "set-var()" converter so if your redirect expressions
> are simple, you could as well set the variable on the fly. It will just not
> take the result of the log-format. Example :
>
> http-request redirect location %[string("/path/to/location"),set-var(txn.location)]
> if { your condition here }
>
> It should be able to always replace my other example above and be more
> efficient. But the example below won't do what you expect :
>
> http-request redirect location /path/to/location/?src=%[src,set-var(txn.location)]
> if { your condition here }
>
> as it would only capture the output of the "src" fetch function instead of
> the whole line. Maybe it could be useful to support an extra "redirect"
> option to duplicate the output into a variable. I don't think it would
> be very complicated to have this :
>
> http-request redirect location /path/to/foo code 302
> set-var(txn.location)
>
> But I could be wrong as I remember that variable allocation at config
> parsing
> time is tricky.
>
> Regards,
> Willy
>
Sorry, only registered users may post in this forum.

Click here to login