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_valid
does not accept variable but there is a work around if you have only two optionscached
andnot-cached
.In this example we want
cached.php
to return the cached version, and the non-cached version if the url contains params. meaningcached.php
will always reaturn the cached version andcached.php?id=1
will always return the non-cached version.This the content of
cached.php
used 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-Cached
is used only for debuging and it is not necessary.