• Support
  • How to Cache few query string.

Users who get redirected from Facebook for my website post, Facebook adds a query string. Facebook adds query string ?fbclid which get BYPASSED by Redis cache due to no query string cache setting. There is no point to BYPASS ?fbclid as the content is ultimately the same.

Could someone help how to cache ?fbclid with Redis cache option of WordOps.

Hello,
In WordOps Nginx configuration, we use nginx map directive and the variable $is_args to detect if there is a query string in the URI requested, and to map this condition with the variable $skip_cache. So it would require to refactor mapping and to replace $is_args by $args to map the exact query string you want to cache.

5 days later

May I suggest a workaround?

For my customers, I created a file /var/www/domain.com/conf/nginx/no-fbclid.conf with:

if ($request_uri ~ "([^\?]*)\?(.*)fbclid=([^&]*)&?(.*)") {
    set $original_path $1;
    set $args1 $2;
    set $unwanted $3;
    set $args2 $4;
    set $args "";

    #rewrite ^ "${original_path}?${args1}${args2}" permanent;
    rewrite ^ "${original_path}" permanent;
}

Instead of caching the URL with the added querystring (which would 100% inefficient, because every single click from FB has a different click ID) I rather redirect the user to the main URL, without any parameter, and totally cacheable.

Perhaps you can try it.

(The commented rewrite should redirect to the same URL just removing the fbclid parameter, but I really don't remember why I prefer to strip the entire querystring.)

5 years later

Hi, I had a similar issue with gclid, gbraid, or other tracking parameters that are unique for each user for tracking purposes.

For example:

domain.com/?gclid=d189h9d8h1892819dh198289ashdakjsdh8912721
domain.com/mypage/?gclid=d189h9d8h1892819dh198289ashdakjsdh8912721
domain.com/my-page-2/?gclid=d189h9d8h1892819dh198289ashdakjsdh8912721

These URLs were missing the fastcgi cache instead of serving the cached page:

domain.com/
domain.com/mypage/
domain.com/my-page-2/

To fix this under my current installation of WordOps v3.21.3 on Ubuntu 20.04.6 LTS:

in each step run nginx -t to check your syntax and when you are done service nginx reload

  1. Edit /etc/nginx/conf.d/map-wp-fastcgi-cache.conf and add the "gclid" parameter.

'# Cache requests with query strings related to analytics
map $args $args_to_cache {
    default 0;
    "~*utm_" 1;
    "~*fbclid" 1;
    "~*gclid" 1;
}
  1. Then, add the following to the bottom of the file:

map $request_uri $cleaned_request_uri {
    ~^(.*)\?.*gclid=.*$ $1; # Remove fbclid parameter if present
    ~^(.*)\?.*fbclid=.*$ $1; # Remove fbclid parameter if present
    default $request_uri;    # Default to original request URI
}

// to work with utm_* needs somo work but also works   ~^(.*)\?(.*&)?(utm_[^&]*)=[^&]*(?:&(.*))?$ $1?$2$4; # Removes only parameters that start with "utm_"
  1. Now edit /etc/nginx/conf.d/fastcgi.conf:

Comment out:

#fastcgi_cache_key "$scheme$request_method$host$request_uri";

Add:

fastcgi_cache_key "$scheme$request_method$host$cleaned_request_uri";
fastcgi_param REQUEST_URI $cleaned_request_uri; # Add this line above
fastcgi_param SERVER_NAME $http_host;
  1. Finally, you have two options: either add a new file under /var/www/domain.com/conf/nginx/cache.conf or edit common/wpfc-php82.conf (note that it will be overwritten when you update WO).

try_files $cleaned_request_uri $uri $uri/ /index.php$is_args$args;

Now, fastCGI will always hit the cache if any parameter (gclid or fbclid) is present. It's kind of a workaround, but it works for me.

However, you can always replace the directives in step 2 with:

~^(.*)\?(.*&)?(fbclid|gclid)=[^&]*(?:&(.*))?$ $1?$2$4; # Removes any of the specified parameters

In each step run nginx -t to check your syntax and when you are done service nginx reload

Hosted by VirtuBox