缓存不存在时,如何禁止 srcache_fetch 输出 cache sent invalid status line 以及 cache sent truncated response body 呢?

189 views
Skip to first unread message

莫言梦

unread,
Sep 20, 2016, 4:58:13 AM9/20/16
to openresty
您好,

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
cache.lua
www.example.com.conf

YuanSheng Wang

unread,
Sep 22, 2016, 11:28:08 AM9/22/16
to open...@googlegroups.com
On Tue, Sep 20, 2016 at 4:58 PM, 莫言梦 <portab...@gmail.com> wrote:
您好,

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 中发现缓存,而输出的这些日志。请问有什么办法禁止此类输出吗?

两个办法:
1. 修改 nginx 错误日志为更高级别,比如 crit 。缺点是所有 error 级别日志都看不到了。
 

2、如何在此 WordPress 环境下将 lua-resty-redis 与 lua-resty-lock 结合起来,加上缓存锁呢?看了 lua-resty-lock 给出的例子,step 4 中 the backend data source 是 Redis 而不是 PHP-FPM,就不知道怎么实现了。谢谢。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html



--

YuanSheng Wang
---------------------------------------
OpenResty lover ^_^

bryant yan

unread,
Sep 5, 2019, 8:51:15 AM9/5/19
to openresty
https://github.com/openresty/lua-resty-redis/tree/v0.2
A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a ngx.null return value.
正确的方式是以下这种
if res == ngx.null then
    ngx.say("dog not found.")
    return
end
原代码中, 这样的写法是不行的
if res == nil
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages