首页 新闻 会员 周边

nginx memcache 不能正常取出key

0
悬赏园豆:30 [已解决问题] 解决于 2015-05-15 18:49

因为项目中的静态资源(HTML,JS,JPG)比较多, 多心考虑台面用nginx搭载memcache增加访问量. 其中nginx.conf中配置(节选如下): 


upstream memcached {
server localhost:11211;
}

server {
listen 4444;
server_name localhost;

location / { #这里面上线时会配置成静态资源
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
charset utf-8;
set $memcached_key $uri;
memcached_pass memcached;
error_page 500 404 405 = @fallback;
}

location @fallback {
root /Users/crystal/www/html;
}



访问http://localhost:4444/a/b/c/static.html资源的时候,后台日志出现


key: "/a/b/c/static.html" was not found by memcached while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /webpro/img/b.html HTTP/1.1", upstream: "memcached://127.0.0.1:11211", host: "localhost:4444".




但我用JAVA的客户端从memcache中读取key为/a/b/c/static.html的值是可以准备提取到对应的资源的, 不知为什么nginx就是读不出来. 后来将


set $memcached_key $uri;
#改成
set $memcached_key 'fixed_key'; #固定的值,不用$uri


立即可以显示正确的页面, 另外发现如果key中包含有'/', 那nginx也不能正常读出.

请问, 大家碰到过这种问题没有

vncnliu的主页 vncnliu | 初学一级 | 园豆:151
提问于:2015-05-15 11:42
< >
分享
最佳答案
0

试试给$uri加上引号:

set $memcached_key "$uri";
收获园豆:30
dudu | 高人七级 |园豆:30994 | 2015-05-15 11:49

再试试set_md5,这个应该不存在"/"问题

set_md5  $memcached_key $uri;

 

dudu | 园豆:30994 (高人七级) | 2015-05-15 11:51
其他回答(1)
1

找到问题了,是使用的memcacheClent是什么dange的,具体我也忘了。这个jar包设置key如果是有"/"它内部不知道是怎么转换的。换成

<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version>
</dependency>

这个就好了,google大法好啊

谢谢@dudu的热心回答

vncnliu | 园豆:151 (初学一级) | 2015-05-15 18:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册