7

I'm using an Nginx cache for an application of mine. I would like to bypass the cache for one very specific request string. I thought I could achieve that using something like the following:

location / {
 if($uri ~ .*(ServerCheck).*) {set $nocache 1; }
 proxy_pass http://127.0.0.1:9001/;
 proxy_no_cache $nocache;
 proxy_cache_bypass $nocache;
 proxy_cache crownstudent;
 proxy_cache_valid 200 1y;
 proxy_cache_use_stale error timeout invalid_header updating
 http_500 http_502 http_503 http_504;
}

However, this doesn't seem to work. Is there any way I can make this happen? The Nginx documentation is very unclear and lacking in examples.

Thanks!

1 Answer 1

6

$uri holds a normalized url, it can be modified by any other directive or by a redirect. Things can be more complicated if your nginx config gets longer & longer. I prefer $request_uri (According to nginx documentation: full original request URI with arguments).

Try to replace your condition with this one:

if($request_uri ~ ServerCheck) {set $nocache 1; }

Btw, You don't need to capture your target string ServerCheck, since nginx just tests the regex.

Also you may want to be sure that request has been replied by proxy. I use add_header directive for this job. Such as:

add_header X-Proxy-Cache $upstream_cache_status;
1
  • 1
    this (both question and answer) deserves more upvotes. Thank you both!
    – jitbit
    Commented May 6, 2019 at 7:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .