问题是 nginx 不获取 memcached 中存在的 memcached 键,每次我请求链接时都会发生这种情况。
memcached 日志:
向 memcached 发出键“site-/links”的 Nginx 请求失败:(但 memcached 中的键数据)
<31 new auto-negotiating client connection
31: Client using the ascii protocol
<31 get site-/links ### NO DATA SEND! but it in cache
>31 END
<31 connection closed.
带有键“site-/links”的 Django 请求成功获取数据
<31 get :1:mkey
>31 sending key :1:mkeys 0 4
mval
(dp1
.
>31 END
<31 get :1:site-/links
>31 sending key :1:site-/links ###data send!
>31 END
<31 set :1:site-/links 0 300 5518
>31 STORED
<31 set :1:mkey 0 300 4
>31 STORED
<31 connection closed.
我用于 memcached 的 nginx 配置文件:
location / {
default_type "text/html; charset=utf-8";
set $memcached_key site-$uri;
memcached_pass 127.0.0.1:11211;
error_page 404 502 = @django;
}
location @django {
include uwsgi_params;
uwsgi_pass unix:///var/tmp/site.sock;
}
django 中间件:
class NginxMemCacheMiddleWare(object):
def process_response(self, request, response):
cacheIt = True
theUrl = request.get_full_path()
# if it's a GET then store it in the cache:
if request.method != 'GET':
cacheIt = False
# loop on our CACHE_INGORE_REGEXPS and ignore
# certain urls.
for exp in settings.CACHE_IGNORE_REGEXPS:
if re.match(exp,theUrl):
cacheIt = False
if cacheIt:
key = '%s-%s' % (settings.CACHE_KEY_PREFIX,theUrl)
#key = theUrl
print "CACHE!"
print key
print "MKEY:",cache.get("mkey")
print cache.get(key)
cache.set(key,response.content)
cache.set("mkey","mval")
return response
那么为什么 nginx 不能使用 memcached 中的密钥获取数据并且总是去 django uwsgi 呢?