I'm trying to do some troubleshooting on a server and would like to setup a conditional header in the event nginx try_files
succeeds to find a file. I currently have nginx setup as a reverse proxy to Apache.
At the moment, I have the following location blocks:
location / {
try_files $uri $uri/ /index.php?$args;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Cache-Bypass $skip_reason;
add_header X-Handled-By $customhandledby;
add_header X-Cache-Bypass $skip_reason;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8080$request_uri;
add_header Front-End-Https on;
proxy_redirect off;
proxy_buffering on;
proxy_cache edge-cache;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_cache_bypass $skip_cache;
proxy_no_cache $skip_cache;
proxy_cache_valid 200 301 302 500m;
proxy_cache_valid 404 1m;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Cache-Bypass $skip_reason;
add_header X-Handled-By $customhandledby;
}
What I would like to have is the add_header X-Handled-By $customhandledby;
to add the header "X-Handled-By nginx" if try_files
succeeds, if it doesn't then it should show "X-Handled-By Apache" - that way I know which server is serving the request.
If I utilise the following map
with proxy_buffering on;
, this works great:
map $upstream_cache_status $customhandledby {
~HIT "nginx";
~MISS "Apache";
~BYPASS "Apache";
~EXPIRED "Apache";
}
but if I turn proxy_buffering off;
then I lose that header for requests that I assume are being handled by nginx?
Is there anything I can do to show "X-Handled-By correct webserver" without relying on nginx caching Apache requests?
default
to yourmap
- but you could also just add a string to theadd_header X-Handled-By
in the firstlocation
block.try_files
fails, it's going toproxy_pass
the request off to Apache - so wouldn't that initial header be incorrect or would it then be overwritten? The easiest way I can think to do it is get rid of all the caching at this point and only apply the relevant header to the relevant block (thelocation ~ \.php$
block = Apache)?