Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Cache By Pass for Query String

Tags:

nginx

What's the best way to exclude a certain query string argument from being cached.

I have a location /news/ where I want /news/xyz to be cached but not /news/xyz?view_story=2022.

I have tried the following:

fastcgi_cache_bypass $arg_view_story;
fastcgi_no_cache $arg_view_story;

Unfortunately that doesn't work.

I have also tried calling Cache configuration only for URLs without query strings but it doesn't work either.

location ~ ^/news/([a-zA-Z0-9]+)$ {

## called Cache configuration 
  
    }

Appreciate any help in advance!

like image 664
JM John Avatar asked Feb 02 '26 17:02

JM John


1 Answers

Finally figured it out.

All you need is two lines:

fastcgi_cache_bypass $is_args;
fastcgi_no_cache $is_args;

Hope this helps anyone else.

like image 195
JM John Avatar answered Feb 05 '26 07:02

JM John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!