您好,
WordPress 环境,使用 srcache + lua-resty-redis + lua-nginx 成功配置了 Nginx 基于 Redis 的缓存,Nginx 先从 Redis 中读取缓存数据,缓存不存在时,再请求后端的 PHP-FPM。
现在有两个问题想要请教下:
1、在缓存不存在时,srcache_fetch 总是输出两行日志:
- *1502086 srcache_fetch: cache sent invalid status line ...
- *1502086 srcache_fetch: cache sent truncated response body ...
我发现并不是出现了什么错误,而是 srcache_fetch 没有在 Redis 中发现缓存,而输出的这些日志。请问有什么办法禁止此类输出吗?
2、如何在此 WordPress 环境下将 lua-resty-redis 与 lua-resty-lock 结合起来,加上缓存锁呢?看了 lua-resty-lock 给出的例子,step 4 中 the backend data source 是 Redis 而不是 PHP-FPM,就不知道怎么实现了。谢谢。
————————————————————————————————————————
以下是我的 Nginx 配置文件:
www.example.com.conf
server {
listen 80;
server_name www.example.com;
access_log access.log;
error_log error.log;
index index.php;
root /path/to/www.example.com;
include wordpress.conf;
# Redis Cache
set $skip_cache 0;
if ($query_string ~* "s=") {
set $skip_cache 1;
}
set $key $request_uri;
if ($request_uri ~ "^(.*)\?(.*)$") {
set $key1 $1;
}
set_md5 $md5_key $key;
set_random $expire 86400 259200;
lua_socket_log_errors off;
location = /redis {
internal;
content_by_lua_file /path/to/cache.lua;
}
location ~ [^/]\.php(/|$) {
# Redis Cache
srcache_fetch_skip $skip_cache;
srcache_store_skip $skip_cache;
srcache_response_cache_control off;
srcache_ignore_content_encoding on;
srcache_fetch GET /redis key=$md5_key;
srcache_store PUT /redis key=$md5_key&expire=$expire;
add_header Cache-Control max-age=86400;
add_header X-Cache $srcache_fetch_status always;
add_header X-Store $srcache_store_status always;
fastcgi_hide_header X-Powered-By;
try_files $uri =404;
fastcgi_pass unix:/path/to/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
其中用到的 cache.lua:
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1500)
local ok, err = red:connect("unix:/path/to/redis.sock")
if not ok then
ngx.log(ngx.ERR, err)
return
end
local method = ngx.req.get_method()
local key = ngx.var.arg_key
if method == "GET" then
local res, flags, err = red:get(key)
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
end
if res == nil and flags == nil and err == nil then
ngx.exit(ngx.HTTP_NOT_FOUND)
end
ngx.print(res)
elseif method == "PUT" then
local value = ngx.req.get_body_data()
local expire = ngx.var.arg_expire or 86400
local ok, err = red:set(key, value, "EX", expire, "NX")
if not ok then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
end
else
ngx.exit(ngx.HTTP_NOT_ALLOWED)
end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(30000, 128)
if not ok then
ngx.log(ngx.ERR, err)
return
end