Nginx | fastcgi_cache_valid dynamic based on request

c0nw0nk nginx-forum at forum.nginx.org
Fri Aug 26 18:17:08 UTC 2016


Maxim Dounin Wrote:
-------------------------------------------------------
> Hello!
> 
> On Fri, Aug 26, 2016 at 11:01:05AM -0400, c0nw0nk wrote:
> 
> > So I have been trying to make the fastcgi_cache_valid value based on
> user
> > request.
> > 
> > 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;
> 
> Exactly, the fastcgi_cache_valid doesn't support variables and 
> that's why it won't work.
> 
> > 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.
> 
> For X-Accel-Expires to work, it must be from an upstream server.  
> That is, using add_header will work if done on your backend, but 
> not in the nginx caching configuration.
> 
> > Is what i am trying to achieve even possible ? From my understand
> the
> > X-Accel-Expires might just be for proxy_cache requests.
> 
> Use separate locations instead, i.e.:
> 
>     location /url1 {
>         fastcgi_cache_valid any 5s;
>         ...
>     }
> 
>     location /url2 {
>         fastcgi_cache_valid any 5m;
>         ...
>     }
> 
> Locations were specifically designed to make distinct URI-based 
> configurations easy and efficient.
> 
> -- 
> Maxim Dounin
> http://nginx.org/
> 
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx


Thanks for the information i don't use proxy_pass and the setup of php is as
shown here.

https://docs.joomla.org/Nginx

But i think based of what you said i should be able to add that same
"X-Accel-Expires" header to my PHP output itself and that will achieve it.
Might even be better since i can define that header on specific pages.

I will give it a try adding it to my PHP script and post back if it works or
not.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,269212,269218#msg-269218



More information about the nginx mailing list