I'm currently working with fastcgi_cache and wanted to pass a variable to fastcgi_cache_valid so I could have variable amount of cache time depending on the file. But it seems that it will not accept a variable.
I tried the following:
set $cache_time 15s;
fastcgi_cache_valid 200 ${cache_time};
fastcgi_cache_valid 200 $cache_time;
set $cache_time "15s";
fastcgi_cache_valid 200 ${cache_time};
fastcgi_cache_valid 200 $cache_time;
set $cache_time 15;
fastcgi_cache_valid 200 ${cache_time}s;
fastcgi_cache_valid 200 $cache_time;
But I receieved the following errors:
nginx: [emerg] invalid time value "$cache_time" in /etc/nginx/conf.d/www.com.conf:118
nginx: [emerg] directive "fastcgi_cache_valid" is not terminated by ";" in /etc/nginx/conf.d/www.com.conf:118
fastcgi_cache_validdoes not accept variable but there is a work around if you have only two optionscachedandnot-cached.In this example we want
cached.phpto return the cached version, and the non-cached version if the url contains params. meaningcached.phpwill always reaturn the cached version andcached.php?id=1will always return the non-cached version.This the content of
cached.phpused for testThis is the results if tests using curl
curl --head "http://www.example.com/cached.php"curl --head "http://www.example.com/cached.php?id=1"X-Is-Cachedis used only for debuging and it is not necessary.