Welcome! Log In Create A New Profile

Advanced

Setting a custom header based on another upstream/sent header

August 22, 2019 08:00AM
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).

---
Maker of Engintron - https://engintron.com
The easy way to integrate Nginx on cPanel as a reverse caching proxy that actually works for any type of website (even forums or e-shops).
Subject Author Posted

Setting a custom header based on another upstream/sent header

fevangelou August 22, 2019 08:00AM

RE: Setting a custom header based on another upstream/sent header

Reinis Rozitis August 22, 2019 08:28AM

Re: RE: Setting a custom header based on another upstream/sent header

fevangelou August 22, 2019 11:25AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 208
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready