Nginx Cache Gclid Ignore

I’ve figured out how to do remove parameters with the set $args syntax, it’s not
pretty though. Would be trivial to do with a parser, but less trivial with
regexp, I basically remove each unwanted GET parameter one-by-one and then fixup
any stray & afterwards:

# remove GET parameters

if ($args ~ (.*)utm_source=[^&]*(.*)) {
set $args $1$2;
}
if ($args ~ (.*)utm_medium=[^&]*(.*)) {
set $args $1$2;
}
if ($args ~ (.*)utm_campaign=[^&]*(.*)) {
set $args $1$2;
}
if ($args ~ (.*)gclid=[^&]*(.*)) {
set $args $1$2;
}
# cleanup any repeated & introduced 
if ($args ~ (.*)&&+(.*)) {
set $args $1&$2;
}
# cleanup leading &
if ($args ~ ^&(.*)) {
set $args $1;
}
# cleanup ending &
if ($args ~ (.*)&$) {
set $args $1;
}

Then proxy_pass and proxy_cache_key must reference $args, thankfully $is_args
appears to work even when changing $args above 🙂

location / {
proxy_pass http://127.0.0.1:1234$uri$is_args$args;
proxy_cache_key "$scheme$host$uri$is_args$args";

Source: https://forum.nginx.org/read.php?2,99098,99445#msg-99445

Leave a Reply