Question

I am having a problem with my $memcached_keys being too long in my .conf file for nginx. I am using the memcached module but some of my urls are too long. I am in the process of trying to user MD5 hashes of the urls instead but in the meantime I was just wondering if there was a way I could check on the length of a string stored in a variable.

so:

set $memcached_key "byp-$uri";
if ($args) {
    set $memcached_key "byp-$uri?$args";
}

if (len($memcache_key) < 250) {
     memcached_pass 127.0.0.1:11211;
     error_page 404 = @cache_miss;
     error_page 502 = @cache_miss;
}
else {
    pass to @cache_miss;
} 
Was it helpful?

Solution

I'm not positive, but I don't think it can be done within the nginx config language. I suspect it would be documented here if it existed, and I don't see anything.

OTHER TIPS

Old question but ....

To do this sort of thing, you need a scripting setup such as the Lua Module:

location / {
    set_by_lua $memcached_key '
        if not ngx.var.args then 
            return "byp-" ..  ngx.var.uri
        else 
            return "byp-" ..  ngx.var.uri .. "?" ..  ngx.var.args
        end
    ';
    content_by_lua '
        local string = string;
        if string.len($memcached_key) < 250 then
            ngx.exec("/memcached");
        else
            ngx.exec("/cache_miss");
        end
    ';
}
location /memcached {
    internal;
    memcached_pass 127.0.0.1:11211;
    error_page 404 = /cache_miss;
    error_page 502 = /cache_miss;
}
location /cache_miss {
    internal;
    ...
}

Suggest "internal" locations instead of named locations due to a few quirks with the latter but named locations can be used as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top