server – nginx – check if try_files succeeded or not

I'm trying to do some troubleshooting on a server and I'd like to install a conditional header in the nginx event try_files manages to find a file. I currently have the nginx configuration as a reverse proxy for 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 disabled;
proxy_buffering on;
proxy_cache edge-cache;
proxy_cache_revalidate on;
wait time error proxy_cache_use_stale invalid_header update 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, otherwise, it should display "X-Handled-By Apache" – this way, I know which server is processing the request.

If I use the following map with proxy_buffering on;it works very well:

map $ upstream_cache_status $ customhandledby {
~ HIT "nginx";
~ MISS "Apache";
~ BYPASS "Apache";
~ EXPIRED "Apache";
}

but if I turn around proxy_buffering off; so I lose this header for the requests that I assume are managed by nginx?

Can I do something to show "X-Handled-By correct web server"Not to mention Apache caching by Nginx?