我的$ memcached_keys在我的.conf文件中对于nginx来说太长了。我正在使用memcached模块,但我的一些网址太长了。我正在尝试使用网址的MD5哈希,但与此同时我只是想知道是否有办法检查存储在变量中的字符串的长度。

这样:

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;
} 
有帮助吗?

解决方案

我不是肯定的,但我不认为它可以在nginx配置语言中完成。我怀疑它会在这里存在,如果它存在,我什么也看不到。

其他提示

老问题,但......

要做这种事情,您需要一个脚本设置,例如Lua模块:

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;
    ...
}

建议“内部”位置而不是命名位置,因为后者有一些怪癖,但也可以使用命名位置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top