Aaron Aaron - 2 months ago 24
jQuery Question

Nginx micro caching with JQuery callback

I have micro caching setup with my Nginx server to cache an API for 2 seconds. However, each time a request is made to the API, a different url is seen by Nginx because of the attached jQuery callback parameter.

Example:

api.example.com/get_heats.php?sheet=105&callback=jQuery222018438785197213292_1475857341748&_=1475857342048


and

api.example.com/get_heats.php?sheet=105&callback=jQuery222018438785197213292_1475857341748&_=1475857342049


should return the same values for 2 seconds because they are both referencing sheet 105, but they don't because the callback and _ parameters are changing with each request. Is there a way for Nginx to ignore any of the other parameters?

Answer

Please try out the following code,

server {
   ...
   location ~ \.php$ {
      ...
      set $cache_key $request_uri;
      ...
      if ($args ~ "sheet") {
         set $cache_key $cache_key|$arg_sheet;
      }
      ...
      fastcgi_cache_key $cache_key;
      ...
   }
   ...
}

References : Learn to implement microcaching, fastcgi_cache_key