Setting a custom header based on another upstream/sent header

fevangelou nginx-forum at forum.nginx.org
Thu Aug 22 12:00:38 UTC 2019


Hi there,

I'm trying (unsuccessfully) to read an upstream/sent response header and set
an additional one based on some regex.

Let's say I want to check if the site served is WordPress. WordPress will
usually output a link header like this:
link: <https://www.domain.com/wp-json/>; rel="https://api.w.org/",
<https://www.domain.com/>; rel=shortlink

So if I did this:
[code]
set $IS_WORDPRESS "false";

# Now lookup "wp-json" in the (response) link header
if ($sent_http_link ~* "wp-json") {
    set $IS_WORDPRESS "true";
}

add_header                    X-Sent-Header "$sent_http_link";
add_header                    X-Is-WordPress $IS_WORDPRESS;
[/code]

You'd probably expect to see 2 headers output here, but in reality you only
get 1:

x-is-wordpress: false

x-sent-header is empty and is not output. Additionally, the regex does not
match at all, that's why x-is-wordpress returns false.

Now dig this. If I comment out the if block, the "$sent_http_link" value is
output just fine

x-sent-header: <https://www.domain.com/wp-json/>; rel="https://api.w.org/",
<https://www.domain.com/>; rel=shortlink
x-is-wordpress: false

It's as if the sent header is nulled if I just call it!

Is this expected behaviour? Could there be another way to do this?

The same happens if I use $upstream_http_X as this is a proxy setup (Nginx
to Apache).

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285363,285363#msg-285363



More information about the nginx mailing list