My goal is to cache assets with query strings with a certain policy and assets that don't have query strings with another. Unfortunately, nginx ignores query strings in location blocks, so I'm stuck using the if
/error_page
strategy helpfully suggested here:
location /static/ {
alias /home/client/public/;
error_page 418 = @long_lived_caching_strategy;
if ($query_string = "example=querystring") {
return 418;
}
}
location @long_lived_caching_strategy {
etag off;
add_header Cache-Control "public";
expires 1y;
}
}
However, my error logs show me that in the above configuration, the alias
directive is ignored. But when I try to move the alias
directive into the @long_lived_caching_strategy
, nginx tells me that alias
isn't allowed there!
What are my options for a workaround?
Alternatively, is there another way to set etag
and add_header
directives differently depending on whether the URL has a query string?
Best Answer
I found an easier solution to my problem thanks to this thread. My goal was really to conditionally add caching-related headers based on whether or not there was a query string, and using
if
to set strings turned out to be the easiest way to achieve that. Here's my final configuration: