0

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?

4
  • You could add a default to your map - but you could also just add a string to the add_header X-Handled-By in the first location block. Mar 29, 2019 at 17:51
  • The cache status header should be empty in this case; you don't need a new header. Mar 30, 2019 at 0:57
  • Thanks @RichardSmith - however if I add the header to the first location block and the try_files fails, it's going to proxy_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 (the location ~ \.php$ block = Apache)? Apr 1, 2019 at 7:48
  • Thanks @MichaelHampton - If I remove all of the caching entirely, how would you propose I identify which server handled the actual processing of the request, Apache or nginx? I could re-enable the server header I guess? Apr 1, 2019 at 7:49

0

You must log in to answer this question.

Browse other questions tagged .