Finding Strength Length in nginx.conf File
I have a problem with my $ memcached_keys being too long in my .conf file for nginx. I am using memcached module, but some of my urls are too long. I'm currently trying to use MD5 hashes for users, but at the same time I'm just wondering if there is a way to check 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;
}
0
source to share
2 answers
Old question, but ....
To do this, you need a scripting setup like 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 "indoor" locations instead of named locations due to a few quirks with the latter, but also named locations.
+3
source to share