2009-05-05 87 views
0

我的$ memcached_keys在我的用於nginx的.conf文件中太長時出現問題。我使用的是memcached模塊,但是我的一些url太長了。我正在嘗試使用URL的MD5哈希值,但同時我只是想知道是否有一種方法可以檢查存儲在變量中的字符串的長度。在nginx.conf文件中查找強度的長度

這樣:

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

我還不能肯定,但我不認爲它可以nginx的配置語言內完成。如果它存在,我懷疑它會被記錄在here,我什麼也沒看到。

3

老問題,但....

要做到這樣的事情,你需要一個腳本設置,如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; 
    ... 
} 

推薦「內部」的位置,而不是因爲命名的位置到後者的一些怪癖,但也可以使用命名的位置。