【问题标题】:Finding the length of a strength in an nginx.conf file在 nginx.conf 文件中查找强度的长度
【发布时间】:2010-10-23 22:58:53
【问题描述】:

我的 $memcached_keys 在我的 .conf 文件中对于 nginx 来说太长了。我正在使用 memcached 模块,但我的一些网址太长了。我正在尝试使用 URL 的 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


    【解决方案1】:

    我不肯定,但我认为它不能在 nginx 配置语言中完成。我怀疑它会被记录在here 如果它存在的话,我什么也没看到。

    【讨论】:

      【解决方案2】:

      老问题,但是....

      要做这种事情,你需要一个脚本设置,比如 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;
          ...
      }
      

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

      【讨论】:

        猜你喜欢
        • 2011-08-27
        • 1970-01-01
        • 2022-01-23
        • 2017-10-01
        • 2013-10-27
        • 2010-11-03
        • 2013-04-11
        • 2018-04-11
        • 1970-01-01
        相关资源
        最近更新 更多