Nginx | fastcgi_cache_valid dynamic based on request

c0nw0nk nginx-forum at
Fri Aug 26 15:01:05 UTC 2016

So I have been trying to make the fastcgi_cache_valid value based on user

if ($request_uri ~ "/url1" ) {
set $cachetime "any 5s";
if ($request_uri ~ "/url2" ) {
set $cachetime "any 5m";

These did not work because it turns out your not allowed to have a dynamic
variable within the fastcgi_cach_valid command.
fastcgi_cache_valid $cachetime;

fastcgi_cache_valid "$cachetime";

They give of this error.
invalid time value "$cachetime"

So instead of the above i tried this instead.

if ($request_uri ~ "/url1" ) {
set $cachetime "5";
if ($request_uri ~ "/url2" ) {
set $cachetime "300";
add_header "X-Accel-Expires" $cachetime;

fastcgi_cache_valid any 60s;

But on url1 i get X-Cache: HIT when it should of expired after 5 seconds.

Is what i am trying to achieve even possible ? From my understand the
X-Accel-Expires might just be for proxy_cache requests.

Posted at Nginx Forum:,269212,269212#msg-269212

More information about the nginx mailing list