{"id":89167,"date":"2026-08-02T15:16:33","date_gmt":"2026-08-02T07:16:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/89167.html"},"modified":"2026-08-02T15:16:33","modified_gmt":"2026-08-02T07:16:33","slug":"nginx%e5%a4%96%e7%bd%ae%e7%bc%93%e5%ad%98-nginx-redis","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/89167.html","title":{"rendered":"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis"},"content":{"rendered":"<h3>\u4e00\u3001\u5f15\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u662f Nginx &#043; Redis&#xff1f;<\/h3>\n<p>\u5728\u300aNginx\u5916\u7f6e\u7f13\u5b58\u300b\u7cfb\u5217\u524d\u6587\u4e2d&#xff0c;\u6211\u4eec\u63a2\u8ba8\u4e86\u5916\u7f6e\u7f13\u5b58\u7684\u67b6\u6784\u9009\u578b\u3001Memcached\u7684\u8f7b\u91cf\u7ea7\u573a\u666f\u4ee5\u53caerror_page\u964d\u7ea7\u4f53\u7cfb\u3002\u800c\u5728\u4f01\u4e1a\u7ea7\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;Nginx &#043; Redis\u00a0\u7ec4\u5408\u5360\u636e\u4e86\u5916\u7f6e\u7f13\u5b5890%\u4ee5\u4e0a\u7684\u4efd\u989d\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;Redis\u4e0d\u4ec5\u662f\u4e00\u4e2aKV\u5b58\u50a8&#xff0c;\u66f4\u662f\u4e00\u4e2a\u652f\u6301\u4e30\u5bcc\u6570\u636e\u7ed3\u6784\u3001\u6301\u4e45\u5316\u3001\u96c6\u7fa4\u6a21\u5f0f\u548c\u53d1\u5e03\u8ba2\u9605\u7684\u901a\u7528\u6570\u636e\u5e73\u53f0\u3002<\/p>\n<p>\u4f46\u201c\u80fd\u7528\u201d\u548c\u201c\u7528\u597d\u201d\u4e4b\u95f4\u9694\u7740\u5de8\u5927\u7684\u9e3f\u6c9f\u3002\u5f88\u591a\u56e2\u961f\u76f4\u63a5\u5c06Nginx\u5bf9\u63a5Redis\u540e&#xff0c;\u53cd\u800c\u5f15\u5165\u4e86\u65b0\u7684\u6027\u80fd\u74f6\u9888\u548c\u7a33\u5b9a\u6027\u98ce\u9669&#xff1a;<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u8bf7\u6c42\u65b0\u5efaTCP\u8fde\u63a5&#xff0c;Redis\u00a0maxclients\u00a0\u8fc5\u901f\u8017\u5c3d&#xff1b;<\/li>\n<li>Lua\u811a\u672c\u4e2d\u672a\u6b63\u786e\u5904\u7406ngx.null&#xff0c;\u5bfc\u81f4\u7f13\u5b58\u7a7f\u900f\u88ab\u8bef\u5224\u4e3a\u547d\u4e2d&#xff1b;<\/li>\n<li>Key\u8bbe\u8ba1\u7f3a\u5c11\u547d\u540d\u7a7a\u95f4\u548c\u7248\u672c\u6807\u8bc6&#xff0c;\u53d1\u5e03\u65f6\u7f13\u5b58\u65e0\u6cd5\u5e73\u6ed1\u5207\u6362&#xff1b;<\/li>\n<li>Redis Cluster\u91cd\u5b9a\u5411&#xff08;MOVED\/ASK&#xff09;\u672a\u5728Lua\u5c42\u5904\u7406&#xff0c;\u96c6\u7fa4\u6269\u7f29\u5bb9\u671f\u95f4\u5927\u91cf500&#xff1b;<\/li>\n<li>\u5e8f\u5217\u5316\u683c\u5f0f\u9009\u62e9\u4e0d\u5f53&#xff0c;JSON\u7f16\u7801\u5f00\u9500\u541e\u566c\u4e86\u7f13\u5b58\u5e26\u6765\u7684\u5ef6\u8fdf\u6536\u76ca\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u95ee\u9898\u7684\u6839\u6e90\u5728\u4e8e&#xff1a;\u628aRedis\u5f53\u4f5c\u4e00\u4e2a\u201c\u8fdc\u7a0b\u53d8\u91cf\u201d\u6765\u7528&#xff0c;\u800c\u975e\u4e00\u4e2a\u9700\u8981\u7cbe\u7ec6\u7ba1\u7406\u7684\u5206\u5e03\u5f0f\u57fa\u7840\u8bbe\u65bd\u3002\u672c\u6587\u5c06\u4ece\u8fde\u63a5\u7ba1\u7406\u3001\u6570\u636e\u4ea4\u4e92\u3001\u96c6\u7fa4\u9002\u914d\u548c\u751f\u4ea7\u8c03\u4f18\u56db\u4e2a\u7ef4\u5ea6&#xff0c;\u7ed9\u51fa\u7ecf\u8fc7\u5927\u89c4\u6a21\u6d41\u91cf\u9a8c\u8bc1\u7684Nginx &#043; Redis\u5916\u7f6e\u7f13\u5b58\u5b8c\u6574\u65b9\u6848\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u67b6\u6784\u5b9a\u4f4d&#xff1a;Nginx &#043; Redis\u5728\u7f13\u5b58\u4f53\u7cfb\u4e2d\u7684\u89d2\u8272<\/h3>\n<h4>2.1 \u4e09\u5c42\u7f13\u5b58\u6a21\u578b\u56de\u987e<\/h4>\n<p>\u5ba2\u6237\u7aef \u2192 Nginx (L1: lua_shared_dict) \u2192 Redis (L2: \u96c6\u7fa4\u5171\u4eab) \u2192 \u6e90\u7ad9 (L3)<br \/>\n           &lt;10\u03bcs                        0.1~0.5ms              1~50ms<\/p>\n<table>\n<tr>\u5c42\u7ea7\u804c\u8d23TTL\u4e00\u81f4\u6027<\/tr>\n<tbody>\n<tr>\n<td>L1<\/td>\n<td>\u62e6\u622a\u70ed\u70b9\u8bf7\u6c42&#xff0c;\u907f\u514d\u7f51\u7edc\u5f00\u9500<\/td>\n<td>3~10s<\/td>\n<td>\u8282\u70b9\u5185\u6700\u7ec8\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td>L2<\/td>\n<td>\u96c6\u7fa4\u5171\u4eab\u7f13\u5b58&#xff0c;\u4fdd\u8bc1\u5168\u5c40\u547d\u4e2d\u7387<\/td>\n<td>30s~30min<\/td>\n<td>\u96c6\u7fa4\u7ea7\u6700\u7ec8\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td>L3<\/td>\n<td>\u6570\u636e\u6743\u5a01\u6765\u6e90<\/td>\n<td>&#8211;<\/td>\n<td>\u5f3a\u4e00\u81f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4cc;\u00a0\u6838\u5fc3\u8ba4\u77e5&#xff1a;Redis\u5728\u5916\u7f6e\u7f13\u5b58\u4f53\u7cfb\u4e2d\u662fL2\u96c6\u7fa4\u5171\u4eab\u5c42\u3002\u5b83\u89e3\u51b3\u7684\u662f\u201c\u591aNginx\u8282\u70b9\u7f13\u5b58\u5b64\u5c9b\u201d\u95ee\u9898&#xff0c;\u800c\u975e\u66ff\u4ee3L1\u672c\u5730\u7f13\u5b58\u3002\u8df3\u8fc7L1\u76f4\u8fdeRedis&#xff0c;\u4f1a\u8ba9Redis\u6210\u4e3a\u65b0\u7684\u5355\u70b9\u74f6\u9888\u3002<\/p>\n<h4>2.2 \u4e3a\u4ec0\u4e48\u4e0d\u7528Nginx\u539f\u751fredis_module&#xff1f;<\/h4>\n<p>\u539f\u751fngx_http_redis_module\u4ec5\u652f\u6301\u53ea\u8bfbGET\u64cd\u4f5c&#xff0c;\u65e0\u8fde\u63a5\u6c60\u3001\u65e0\u5199\u5165\u3001\u65e0\u5220\u9664\u3001\u65e0Cluster\u652f\u6301\u3002\u751f\u4ea7\u73af\u5883\u5fc5\u987b\u4f7f\u7528OpenResty\u751f\u6001\u7684lua-resty-redis\u5e93&#xff0c;\u5b83\u63d0\u4f9b\u4e86&#xff1a;<\/p>\n<ul>\n<li>\u2705 \u5b8c\u6574\u7684Redis\u547d\u4ee4\u652f\u6301&#xff08;GET\/SET\/HGET\/DEL\/SCAN\u7b49&#xff09;<\/li>\n<li>\u2705 \u57fa\u4e8ecosocket\u7684\u975e\u963b\u585eIO<\/li>\n<li>\u2705 \u8fde\u63a5\u6c60\u590d\u7528&#xff08;set_keepalive&#xff09;<\/li>\n<li>\u2705 Redis Cluster MOVED\/ASK\u81ea\u52a8\u91cd\u5b9a\u5411&#xff08;\u9700\u914d\u5408lua-resty-redis-cluster&#xff09;<\/li>\n<li>\u2705 Pipeline\u6279\u91cf\u64cd\u4f5c<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e09\u3001lua-resty-redis \u751f\u4ea7\u7ea7\u5c01\u88c5<\/h3>\n<h4>3.1 \u6838\u5fc3\u6a21\u5757\u8bbe\u8ba1<\/h4>\n<p>\u521b\u5efa\u00a0\/usr\/local\/openresty\/lualib\/redis_cache.lua&#xff1a;<\/p>\n<p>local redis &#061; require &#034;resty.redis&#034;<br \/>\nlocal cjson &#061; require &#034;cjson.safe&#034;<\/p>\n<p>local _M &#061; {}<\/p>\n<p>&#8212; \u2b50 \u8fde\u63a5\u6c60\u914d\u7f6e&#xff08;\u6839\u636e\u5b9e\u9645\u538b\u6d4b\u8c03\u6574&#xff09;<br \/>\nlocal CONF &#061; {<br \/>\n    host &#061; &#034;redis-cluster.internal&#034;,<br \/>\n    port &#061; 6379,<br \/>\n    pool_size &#061; 100,         &#8212; \u6bcfworker\u8fde\u63a5\u6c60\u5927\u5c0f<br \/>\n    backlog &#061; 200,           &#8212; \u7b49\u5f85\u961f\u5217\u957f\u5ea6<br \/>\n    connect_timeout &#061; 100,   &#8212; ms<br \/>\n    send_timeout &#061; 200,      &#8212; ms<br \/>\n    read_timeout &#061; 200,      &#8212; ms<br \/>\n}<\/p>\n<p>&#8212; \u83b7\u53d6Redis\u8fde\u63a5&#xff08;\u5e26\u8fde\u63a5\u6c60&#xff09;<br \/>\nlocal function get_redis()<br \/>\n    local red, err &#061; redis:new()<br \/>\n    if not red then<br \/>\n        ngx.log(ngx.ERR, &#034;redis new failed: &#034;, err)<br \/>\n        return nil, err<br \/>\n    end<\/p>\n<p>    red:set_timeouts(CONF.connect_timeout, CONF.send_timeout, CONF.read_timeout)<\/p>\n<p>    local ok, err &#061; red:connect(CONF.host, CONF.port)<br \/>\n    if not ok then<br \/>\n        ngx.log(ngx.ERR, &#034;redis connect failed: &#034;, err)<br \/>\n        return nil, err<br \/>\n    end<\/p>\n<p>    &#8212; \u5982\u9700\u5bc6\u7801\u8ba4\u8bc1<br \/>\n    &#8212; local auth_ok, auth_err &#061; red:auth(&#034;your_password&#034;)<br \/>\n    &#8212; if not auth_ok then return nil, auth_err end<\/p>\n<p>    return red, nil<br \/>\nend<\/p>\n<p>&#8212; \u2b50 \u91ca\u653e\u8fde\u63a5\u5230\u6c60\u4e2d&#xff08;\u5173\u952e&#xff01;&#xff09;<br \/>\nlocal function release_redis(red)<br \/>\n    local ok, err &#061; red:set_keepalive(10000, CONF.pool_size)<br \/>\n    if not ok then<br \/>\n        ngx.log(ngx.ERR, &#034;redis keepalive failed: &#034;, err)<br \/>\n    end<br \/>\nend<\/p>\n<p>&#8212; \u8bfb\u53d6\u7f13\u5b58<br \/>\nfunction _M.get(key)<br \/>\n    local red, err &#061; get_redis()<br \/>\n    if not red then return nil, err end<\/p>\n<p>    local res, err &#061; red:get(key)<br \/>\n    release_redis(red)<\/p>\n<p>    &#8212; \u2b50 \u6b63\u786e\u533a\u5206MISS\u548cERROR<br \/>\n    if not res then<br \/>\n        return nil, err  &#8212; \u7f51\u7edc\/\u534f\u8bae\u9519\u8bef<br \/>\n    end<br \/>\n    if res &#061;&#061; ngx.null then<br \/>\n        return nil, &#034;MISS&#034;  &#8212; Key\u4e0d\u5b58\u5728<br \/>\n    end<\/p>\n<p>    return res, nil<br \/>\nend<\/p>\n<p>&#8212; \u5199\u5165\u7f13\u5b58&#xff08;\u5e26TTL&#xff09;<br \/>\nfunction _M.set(key, value, ttl)<br \/>\n    local red, err &#061; get_redis()<br \/>\n    if not red then return false, err end<\/p>\n<p>    local ok, err &#061; red:setex(key, ttl or 300, value)<br \/>\n    release_redis(red)<\/p>\n<p>    return ok ~&#061; nil, err<br \/>\nend<\/p>\n<p>&#8212; \u5220\u9664\u7f13\u5b58<br \/>\nfunction _M.delete(key)<br \/>\n    local red, err &#061; get_redis()<br \/>\n    if not red then return false, err end<\/p>\n<p>    local ok, err &#061; red:del(key)<br \/>\n    release_redis(red)<\/p>\n<p>    return ok ~&#061; nil, err<br \/>\nend<\/p>\n<p>&#8212; \u2b50 \u5b89\u5168\u6279\u91cf\u5220\u9664&#xff08;SCAN\u66ff\u4ee3KEYS&#xff09;<br \/>\nfunction _M.delete_pattern(pattern)<br \/>\n    local red, err &#061; get_redis()<br \/>\n    if not red then return false, err end<\/p>\n<p>    local cursor &#061; &#034;0&#034;<br \/>\n    local total &#061; 0<br \/>\n    repeat<br \/>\n        local res, err &#061; red:scan(cursor, &#034;MATCH&#034;, pattern, &#034;COUNT&#034;, 100)<br \/>\n        if not res then<br \/>\n            release_redis(red)<br \/>\n            return false, err<br \/>\n        end<\/p>\n<p>        cursor &#061; res[1]<br \/>\n        local keys &#061; res[2]<br \/>\n        if #keys &gt; 0 then<br \/>\n            local del_ok, del_err &#061; red:del(unpack(keys))<br \/>\n            if del_ok then total &#061; total &#043; del_ok end<br \/>\n        end<br \/>\n    until cursor &#061;&#061; &#034;0&#034;<\/p>\n<p>    release_redis(red)<br \/>\n    return true, nil, total<br \/>\nend<\/p>\n<p>return _M<\/p>\n<h4>3.2 \u4e09\u4e2a\u81f4\u547d\u7ec6\u8282\u89e3\u6790<\/h4>\n<h5>\u2460 ngx.null \u2260 nil<\/h5>\n<p>&#8212; \u274c \u9519\u8bef&#xff1a;\u5c06MISS\u5f53\u4f5c\u6210\u529f<br \/>\nlocal res &#061; red:get(key)<br \/>\nif res then  &#8212; ngx.null \u662ftruthy&#xff01;\u4f1a\u8fdb\u5165\u6b64\u5206\u652f<br \/>\n    ngx.say(res)  &#8212; \u8f93\u51fa &#034;null&#034; \u5b57\u7b26\u4e32<br \/>\nend<\/p>\n<p>&#8212; \u2705 \u6b63\u786e&#xff1a;\u663e\u5f0f\u5224\u65adngx.null<br \/>\nif res &#061;&#061; ngx.null then<br \/>\n    &#8212; MISS<br \/>\nelseif not res then<br \/>\n    &#8212; ERROR<br \/>\nelse<br \/>\n    &#8212; HIT<br \/>\nend<\/p>\n<p>\u26a0\ufe0f\u00a0\u8fd9\u662fNginx&#043;Redis\u6700\u5e38\u89c1\u7684Bug\u3002ngx.null\u662fLua\u4e2d\u7684\u7279\u6b8a\u5bf9\u8c61&#xff0c;\u8868\u793aRedis\u8fd4\u56de\u7684NIL\u503c\u3002\u5b83\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u4e3atrue&#xff0c;\u76f4\u63a5\u5f53\u5b57\u7b26\u4e32\u4f7f\u7528\u4f1a\u5bfc\u81f4\u4e0b\u6e38\u89e3\u6790\u5931\u8d25\u3002<\/p>\n<h5>\u2461 set_keepalive\u5fc5\u987b\u5728\u6bcf\u6b21\u64cd\u4f5c\u540e\u8c03\u7528<\/h5>\n<p>&#8212; \u274c \u9519\u8bef&#xff1a;\u5f02\u5e38\u8def\u5f84\u672a\u91ca\u653e\u8fde\u63a5<br \/>\nlocal res, err &#061; red:get(key)<br \/>\nif not res then<br \/>\n    return nil, err  &#8212; \u8fde\u63a5\u6cc4\u6f0f&#xff01;<br \/>\nend<br \/>\nrelease_redis(red)<\/p>\n<p>&#8212; \u2705 \u6b63\u786e&#xff1a;\u6240\u6709\u9000\u51fa\u8def\u5f84\u90fd\u91ca\u653e<br \/>\nlocal res, err &#061; red:get(key)<br \/>\nrelease_redis(red)  &#8212; \u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u91ca\u653e<br \/>\nif not res then return nil, err end<\/p>\n<h5>\u2462 SCAN\u66ff\u4ee3KEYS\u662f\u94c1\u5f8b<\/h5>\n<p>KEYS *pattern*\u00a0\u662fO(N)\u5168\u91cf\u626b\u63cf&#xff0c;\u5728\u751f\u4ea7Redis\u4e0a\u6267\u884c\u7b49\u540c\u4e8eDDoS\u653b\u51fb\u3002SCAN\u6e38\u6807\u904d\u5386\u662f\u552f\u4e00\u5b89\u5168\u7684\u6279\u91cf\u64cd\u4f5c\u65b9\u5f0f&#xff0c;\u5373\u4f7f\u5339\u914d\u7ed3\u679c\u4e3a\u7a7a\u4e5f\u5fc5\u987b\u5b8c\u6574\u904d\u5386\u76f4\u5230cursor\u8fd4\u56de&#034;0&#034;\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001Nginx\u914d\u7f6e\u96c6\u6210\u4e0e\u4e24\u5c42\u7f13\u5b58\u8054\u52a8<\/h3>\n<h4>4.1 \u5b8c\u6574\u914d\u7f6e\u793a\u4f8b<\/h4>\n<p>http {<br \/>\n    # L1\u672c\u5730\u7f13\u5b58 &#043; \u5206\u5e03\u5f0f\u9501<br \/>\n    lua_shared_dict l1_cache 100m;<br \/>\n    lua_shared_dict cache_locks 10m;<\/p>\n<p>    init_by_lua_block {<br \/>\n        redis_cache &#061; require &#034;redis_cache&#034;<br \/>\n        cjson &#061; require &#034;cjson.safe&#034;<br \/>\n    }<\/p>\n<p>    server {<br \/>\n        listen 80;<\/p>\n<p>        location \/api\/ {<br \/>\n            content_by_lua_block {<br \/>\n                local key &#061; &#034;api:v2:&#034; .. ngx.var.uri<\/p>\n<p>                &#8212; &#061;&#061;&#061;&#061;&#061; L1: \u672c\u5730\u5171\u4eab\u5b57\u5178 &#061;&#061;&#061;&#061;&#061;<br \/>\n                local l1 &#061; ngx.shared.l1_cache<br \/>\n                local l1_val &#061; l1:get(key)<br \/>\n                if l1_val then<br \/>\n                    ngx.header[&#034;X-Cache&#034;] &#061; &#034;L1-HIT&#034;<br \/>\n                    ngx.say(l1_val)<br \/>\n                    return<br \/>\n                end<\/p>\n<p>                &#8212; &#061;&#061;&#061;&#061;&#061; L2: Redis &#061;&#061;&#061;&#061;&#061;<br \/>\n                local data, err &#061; redis_cache.get(key)<br \/>\n                if err and err ~&#061; &#034;MISS&#034; then<br \/>\n                    ngx.log(ngx.WARN, &#034;redis error, fallback to backend: &#034;, err)<br \/>\n                    &#8212; \u964d\u7ea7&#xff1a;\u8df3\u8fc7\u7f13\u5b58\u76f4\u63a5\u56de\u6e90<br \/>\n                    goto backend<br \/>\n                end<\/p>\n<p>                if err ~&#061; &#034;MISS&#034; then<br \/>\n                    &#8212; Redis HIT \u2192 \u56de\u586bL1<br \/>\n                    l1:set(key, data, 5)<br \/>\n                    ngx.header[&#034;X-Cache&#034;] &#061; &#034;L2-HIT&#034;<br \/>\n                    ngx.say(data)<br \/>\n                    return<br \/>\n                end<\/p>\n<p>                &#8212; &#061;&#061;&#061;&#061;&#061; MISS: \u9632\u51fb\u7a7f &#043; \u56de\u6e90 &#061;&#061;&#061;&#061;&#061;<br \/>\n                ::backend::<br \/>\n                local locks &#061; ngx.shared.cache_locks<br \/>\n                local lock_key &#061; &#034;lock:&#034; .. key<br \/>\n                local elapsed, lerr &#061; locks:add(lock_key, true, 3)<\/p>\n<p>                if elapsed then<br \/>\n                    local res &#061; ngx.location.capture(&#034;\/internal\/backend&#034;)<br \/>\n                    if res.status &#061;&#061; 200 then<br \/>\n                        redis_cache.set(key, res.body, 300)<br \/>\n                        l1:set(key, res.body, 5)<br \/>\n                        ngx.header[&#034;X-Cache&#034;] &#061; &#034;MISS&#034;<br \/>\n                        ngx.say(res.body)<br \/>\n                    else<br \/>\n                        ngx.status &#061; res.status<br \/>\n                        ngx.say(res.body)<br \/>\n                    end<br \/>\n                    locks:delete(lock_key)<br \/>\n                else<br \/>\n                    &#8212; \u672a\u83b7\u9501&#xff1a;\u77ed\u6682\u7b49\u5f85\u540e\u91cd\u8bd5<br \/>\n                    ngx.sleep(0.1)<br \/>\n                    local retry, rerr &#061; redis_cache.get(key)<br \/>\n                    if retry and rerr ~&#061; &#034;MISS&#034; then<br \/>\n                        ngx.header[&#034;X-Cache&#034;] &#061; &#034;LOCK-WAIT-HIT&#034;<br \/>\n                        ngx.say(retry)<br \/>\n                    else<br \/>\n                        &#8212; \u964d\u7ea7\u56de\u6e90&#xff08;\u4e0d\u7f13\u5b58&#xff09;<br \/>\n                        local res &#061; ngx.location.capture(&#034;\/internal\/backend&#034;)<br \/>\n                        ngx.header[&#034;X-Cache&#034;] &#061; &#034;BYPASS&#034;<br \/>\n                        ngx.status &#061; res.status<br \/>\n                        ngx.say(res.body)<br \/>\n                    end<br \/>\n                end<br \/>\n            }<br \/>\n        }<\/p>\n<p>        location \/internal\/backend {<br \/>\n            internal;<br \/>\n            proxy_pass http:\/\/backend;<br \/>\n            proxy_set_header Host $host;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>4.2 goto\u8bed\u6cd5\u7684\u5408\u7406\u4f7f\u7528<\/h4>\n<p>OpenResty\u7684LuaJIT\u652f\u6301goto&#xff0c;\u5728\u7f13\u5b58\u903b\u8f91\u8fd9\u79cd\u591a\u5206\u652f\u8df3\u8f6c\u573a\u666f\u4e2d&#xff0c;\u6bd4\u5d4c\u5957if-else\u6216\u91cd\u590d\u4ee3\u7801\u66f4\u6e05\u6670\u3002\u4f46\u4ec5\u9650\u7f13\u5b58\u6d41\u7a0b\u63a7\u5236\u4f7f\u7528&#xff0c;\u4e1a\u52a1\u903b\u8f91\u4e2d\u5e94\u907f\u514d\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001Redis Cluster\u9002\u914d<\/h3>\n<h4>5.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u4e13\u95e8\u5904\u7406&#xff1f;<\/h4>\n<p>Redis Cluster\u901a\u8fc7\u54c8\u5e0c\u69fd\u5206\u7247&#xff0c;\u5f53Key\u6240\u5728\u69fd\u8fc1\u79fb\u65f6&#xff0c;\u670d\u52a1\u7aef\u8fd4\u56deMOVED\u6216ASK\u91cd\u5b9a\u5411\u3002lua-resty-redis\u539f\u751f\u4e0d\u5904\u7406\u91cd\u5b9a\u5411&#xff0c;\u9700\u4f7f\u7528lua-resty-redis-cluster\u5e93&#xff1a;<\/p>\n<p>local redis_cluster &#061; require &#034;resty.rediscluster&#034;<\/p>\n<p>local config &#061; {<br \/>\n    name &#061; &#034;my_cluster&#034;,<br \/>\n    serv_list &#061; {<br \/>\n        { ip &#061; &#034;10.0.0.1&#034;, port &#061; 6379 },<br \/>\n        { ip &#061; &#034;10.0.0.2&#034;, port &#061; 6379 },<br \/>\n        { ip &#061; &#034;10.0.0.3&#034;, port &#061; 6379 },<br \/>\n    },<br \/>\n    keepalive_timeout &#061; 10000,<br \/>\n    keepalive_poolsize &#061; 100,<br \/>\n    connect_timeout &#061; 100,<br \/>\n    read_timeout &#061; 200,<br \/>\n    max_redirection &#061; 5,  &#8212; \u2b50 \u6700\u5927\u91cd\u5b9a\u5411\u6b21\u6570<br \/>\n}<\/p>\n<p>local cluster, err &#061; redis_cluster:new(config)<br \/>\n&#8212; \u540e\u7eedAPI\u4e0elua-resty-redis\u517c\u5bb9&#xff1a;cluster:get(key), cluster:set(key, val, ttl)<\/p>\n<h4>5.2 Cluster\u6ce8\u610f\u4e8b\u9879<\/h4>\n<table>\n<tr>\u8981\u70b9\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u4e0d\u652f\u6301\u591aKey\u8de8\u69fd\u64cd\u4f5c<\/td>\n<td>MGET\/MSET\u7684Key\u5fc5\u987b\u5728\u540c\u4e00\u69fd&#xff0c;\u5426\u5219\u62a5\u9519<\/td>\n<\/tr>\n<tr>\n<td>SCAN\u6309\u8282\u70b9\u6267\u884c<\/td>\n<td>\u9700\u904d\u5386\u6240\u6709master\u8282\u70b9\u624d\u80fd\u5b8c\u6210\u5168\u91cf\u626b\u63cf<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u6c60\u6309\u8282\u70b9\u72ec\u7acb<\/td>\n<td>\u6bcf\u4e2a\u8282\u70b9\u7ef4\u62a4\u72ec\u7acbpool&#xff0c;\u603b\u8fde\u63a5&#061;\u8282\u70b9\u6570\u00d7pool_size<\/td>\n<\/tr>\n<tr>\n<td>\u62d3\u6251\u53d8\u66f4\u81ea\u52a8\u611f\u77e5<\/td>\n<td>\u5e93\u4f1a\u81ea\u52a8\u5237\u65b0slot\u6620\u5c04&#xff0c;\u65e0\u9700\u91cd\u542fNginx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u907f\u5751&#xff1a;\u82e5\u4f60\u7684Key\u8bbe\u8ba1\u5929\u7136\u5206\u6563\u5728\u4e0d\u540c\u69fd&#xff0c;\u907f\u514d\u4f7f\u7528MGET\u3002\u6539\u4e3aPipeline\u5355\u8282\u70b9\u8bf7\u6c42\u6216\u5e94\u7528\u5c42\u5e76\u53d1\u83b7\u53d6\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u5e8f\u5217\u5316\u683c\u5f0f\u9009\u578b<\/h3>\n<h4>6.1 \u6027\u80fd\u5bf9\u6bd4\u5b9e\u6d4b&#xff08;1KB JSON\u5bf9\u8c61&#xff09;<\/h4>\n<table>\n<tr>\u683c\u5f0f\u7f16\u7801\u8017\u65f6\u89e3\u7801\u8017\u65f6\u4f53\u79ef\u8de8\u8bed\u8a00\u63a8\u8350\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>cjson<\/td>\n<td>\u57fa\u51c6<\/td>\n<td>\u57fa\u51c6<\/td>\n<td>\u57fa\u51c6<\/td>\n<td>\u2705<\/td>\n<td>\u901a\u7528API\u54cd\u5e94<\/td>\n<\/tr>\n<tr>\n<td>MessagePack<\/td>\n<td>-30%<\/td>\n<td>-25%<\/td>\n<td>-20%<\/td>\n<td>\u2705<\/td>\n<td>\u9ad8\u9891\u5185\u90e8\u901a\u4fe1<\/td>\n<\/tr>\n<tr>\n<td>Protobuf<\/td>\n<td>-60%<\/td>\n<td>-50%<\/td>\n<td>-40%<\/td>\n<td>\u2705<\/td>\n<td>\u7ed3\u6784\u5316\u6570\u636e\u3001\u5e26\u5bbd\u654f\u611f<\/td>\n<\/tr>\n<tr>\n<td>Lua table\u5e8f\u5217\u5316<\/td>\n<td>-70%<\/td>\n<td>-60%<\/td>\n<td>-50%<\/td>\n<td>\u274c<\/td>\n<td>\u4ec5OpenResty\u5185\u90e8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>6.2 \u4e8c\u8fdb\u5236\u5b89\u5168\u8b66\u544a<\/h4>\n<p>&#8212; \u274c JSON\u65e0\u6cd5\u5b89\u5168\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e<br \/>\ncjson.encode({ body &#061; &#034;\\\\xff\\\\xfe\\\\x00&#034; })  &#8212; \u53ef\u80fd\u635f\u574f<\/p>\n<p>&#8212; \u2705 \u4e8c\u8fdb\u5236\u5185\u5bb9\u4f7f\u7528MessagePack\u6216\u76f4\u63a5\u5b58raw string<br \/>\nmp.encode({ body &#061; binary_data })<br \/>\n&#8212; \u6216<br \/>\nredis:set(key, binary_data, ttl)  &#8212; Memcached\/Redis\u5747\u4e8c\u8fdb\u5236\u5b89\u5168<\/p>\n<hr \/>\n<h3>\u4e03\u3001Key\u8bbe\u8ba1\u89c4\u8303<\/h3>\n<h4>7.1 \u547d\u540d\u89c4\u8303<\/h4>\n<p>&#8212; \u2705 \u63a8\u8350\u683c\u5f0f&#xff1a;{namespace}:{version}:{entity}:{id}<br \/>\n&#034;api:v2:user:profile:10086&#034;<br \/>\n&#034;sess:v1:token:abc123def456&#034;<br \/>\n&#034;cnt:v1:api:\/users\/list:daily:20260802&#034;<\/p>\n<p>&#8212; \u274c \u907f\u514d<br \/>\n&#034;\/api\/user\/profile?id&#061;10086&#034;     &#8212; URI\u4f5cKey&#xff0c;\u53c2\u6570\u987a\u5e8f\u53d8\u5316\u5bfc\u81f4\u91cd\u590d<br \/>\n&#034;user_10086&#034;                     &#8212; \u65e0\u547d\u540d\u7a7a\u95f4&#xff0c;\u6613\u51b2\u7a81<br \/>\n&#034;a&#034;*300                          &#8212; \u8d85\u957fKey\u6d6a\u8d39\u5185\u5b58\u548c\u7f51\u7edc<\/p>\n<h4>7.2 \u8bbe\u8ba1\u539f\u5219<\/h4>\n<table>\n<tr>\u539f\u5219\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u53ef\u8bfb\u6027<\/td>\n<td>\u4fbf\u4e8e\u8c03\u8bd5\u3001\u624b\u52a8\u6e05\u7406\u548c\u76d1\u63a7\u5206\u6790<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00\u6027<\/td>\n<td>\u5305\u542b\u5f71\u54cd\u54cd\u5e94\u5185\u5bb9\u7684\u6240\u6709\u53d8\u91cf&#xff08;method\/args\/header&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u6f14\u8fdb\u6027<\/td>\n<td>\u5d4c\u5165\u7248\u672c\u53f7&#xff0c;\u53d1\u5e03\u65f6\u53ef\u5e73\u6ed1\u5207\u6362\u65e7\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td>\u957f\u5ea6\u63a7\u5236<\/td>\n<td>&lt;256\u5b57\u8282&#xff0c;\u8fc7\u957f\u6d6a\u8d39Redis\u5185\u5b58\u548c\u7f51\u7edc\u5e26\u5bbd<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u679a\u4e3e\u6027<\/td>\n<td>\u652f\u6301SCAN\u6309\u524d\u7f00\u6279\u91cf\u64cd\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u516b\u3001\u751f\u4ea7\u8c03\u4f18\u6e05\u5355<\/h3>\n<h4>8.1 \u8fde\u63a5\u6c60Sizing<\/h4>\n<p>\u603b\u5e76\u53d1Redis\u8fde\u63a5 &#061; Nginx worker\u6570 \u00d7 pool_size<br \/>\n\u793a\u4f8b&#xff1a;worker&#061;8, pool_size&#061;100 \u2192 \u603b\u8fde\u63a5&#061;800<br \/>\nRedis maxclients \u5e94 \u2265 800 \u00d7 1.5 &#061; 1200<\/p>\n<p>\u901a\u8fc7INFO clients\u76d1\u63a7connected_clients&#xff0c;\u5cf0\u503c\u63a5\u8fd1maxclients\u65f6\u9700\u6269\u5bb9\u6216\u8c03\u5927pool\u3002<\/p>\n<h4>8.2 \u8d85\u65f6\u8bbe\u7f6e\u7b56\u7565<\/h4>\n<table>\n<tr>\u9636\u6bb5\u63a8\u8350\u503c\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>connect_timeout<\/td>\n<td>100ms<\/td>\n<td>\u5feb\u901f\u5931\u8d25&#xff0c;\u907f\u514d\u963b\u585eworker<\/td>\n<\/tr>\n<tr>\n<td>read_timeout<\/td>\n<td>200ms<\/td>\n<td>P99\u5e94&lt;1ms&#xff0c;200ms\u5df2\u662f\u5bb9\u5fcd\u4e0a\u9650<\/td>\n<\/tr>\n<tr>\n<td>send_timeout<\/td>\n<td>200ms<\/td>\n<td>\u5927Value\u5199\u5165\u65f6\u9002\u5f53\u653e\u5bbd<\/td>\n<\/tr>\n<tr>\n<td>keepalive_idle<\/td>\n<td>10s<\/td>\n<td>\u7a7a\u95f2\u8fde\u63a5\u4fdd\u6d3b\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u94c1\u5f8b&#xff1a;Redis\u64cd\u4f5c\u8d85\u65f6\u5fc5\u987b\u8fdc\u5c0f\u4e8eNginx\u5bf9\u5ba2\u6237\u7aef\u7684\u8d85\u65f6\u3002\u82e5\u5ba2\u6237\u7aef\u8d85\u65f65s&#xff0c;Redis\u8d85\u65f6\u8bbe\u4e3a200ms&#xff0c;\u7559\u51fa\u8db3\u591f\u7684\u964d\u7ea7\u548c\u91cd\u8bd5\u7a97\u53e3\u3002<\/p>\n<h4>8.3 \u6545\u969c\u964d\u7ea7<\/h4>\n<p>local data, err &#061; redis_cache.get(key)<br \/>\nif err and err ~&#061; &#034;MISS&#034; then<br \/>\n    ngx.log(ngx.WARN, &#034;redis degraded: &#034;, err)<br \/>\n    &#8212; \u9009\u98791&#xff1a;\u8df3\u8fc7\u7f13\u5b58&#xff0c;\u76f4\u63a5\u56de\u6e90<br \/>\n    &#8212; \u9009\u98792&#xff1a;\u8fd4\u56deL1\u8fc7\u671f\u6570\u636e\u4f5c\u4e3a\u515c\u5e95<br \/>\n    &#8212; \u9009\u98793&#xff1a;\u8fd4\u56de\u9884\u8bbe\u9ed8\u8ba4\u503c<br \/>\n    &#8212; \u2b50 \u6c38\u8fdc\u4e0d\u8981\u8ba9\u7f13\u5b58\u6545\u969c\u53d8\u6210\u670d\u52a1\u6545\u969c<br \/>\nend<\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u76d1\u63a7\u6307\u6807\u4f53\u7cfb<\/h3>\n<h4>9.1 \u5fc5\u91c7\u6307\u6807<\/h4>\n<table>\n<tr>\u6307\u6807\u6765\u6e90\u5065\u5eb7\u9608\u503c\u544a\u8b66\u7ea7\u522b<\/tr>\n<tbody>\n<tr>\n<td>L1 HIT\u7387<\/td>\n<td>lua_shared_dict stats<\/td>\n<td>\u70ed\u70b9\u63a5\u53e3&gt;30%<\/td>\n<td>P3<\/td>\n<\/tr>\n<tr>\n<td>L2 HIT\u7387<\/td>\n<td>Redis INFO stats<\/td>\n<td>&gt;60%<\/td>\n<td>P2<\/td>\n<\/tr>\n<tr>\n<td>Redis P99\u5ef6\u8fdf<\/td>\n<td>redis_exporter<\/td>\n<td>&lt;1ms<\/td>\n<td>P2<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u6c60\u4f7f\u7528\u7387<\/td>\n<td>shared_dict stats<\/td>\n<td>&lt;80%<\/td>\n<td>P2<\/td>\n<\/tr>\n<tr>\n<td>Redis\u9519\u8bef\u7387<\/td>\n<td>error.log\u805a\u5408<\/td>\n<td>&lt;0.1%<\/td>\n<td>P1<\/td>\n<\/tr>\n<tr>\n<td>Redis\u5185\u5b58\u4f7f\u7528\u7387<\/td>\n<td>redis_exporter<\/td>\n<td>&lt;75%<\/td>\n<td>P2<\/td>\n<\/tr>\n<tr>\n<td>Redis\u9a71\u9010\u7387<\/td>\n<td>INFO stats evicted_keys<\/td>\n<td>&#061;0<\/td>\n<td>P1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>9.2 Grafana\u6838\u5fc3\u9762\u677f<\/h4>\n<ul>\n<li>\u7f13\u5b58\u6f0f\u6597\u56fe&#xff1a;Request \u2192 L1 HIT \u2192 L2 HIT \u2192 Backend&#xff0c;\u76f4\u89c2\u5c55\u793a\u5404\u5c42\u62e6\u622a\u6548\u679c<\/li>\n<li>Redis\u5ef6\u8fdf\u70ed\u529b\u56fe&#xff1a;\u6309\u65f6\u95f4\u6bb5\u548c\u547d\u4ee4\u7c7b\u578b\u5206\u5e03&#xff0c;\u5b9a\u4f4d\u6162\u67e5\u8be2<\/li>\n<li>\u8fde\u63a5\u6c60\u6c34\u4f4d\u66f2\u7ebf&#xff1a;\u5cf0\u503c\u662f\u5426\u63a5\u8fd1pool_size\u4e0a\u9650<\/li>\n<li>\u9519\u8bef\u7387\u8d8b\u52bf&#xff1a;\u7a81\u589e\u662f\u5426\u5173\u8054\u53d1\u5e03\u6216Redis\u6545\u969c<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5341\u3001\u5e38\u89c1\u8e29\u5751\u901f\u67e5\u8868<\/h3>\n<table>\n<tr>\u73b0\u8c61\u6839\u56e0\u89e3\u51b3\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>Redis\u8fde\u63a5\u8017\u5c3d<\/td>\n<td>\u672a\u7528\u8fde\u63a5\u6c60\u6216pool_size\u8fc7\u5c0f<\/td>\n<td>set_keepalive &#043; \u589e\u5927pool_size<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u547d\u4e2d\u4f46\u6570\u636e\u9519\u4e71<\/td>\n<td>ngx.null\u672a\u6b63\u786e\u5904\u7406<\/td>\n<td>\u663e\u5f0f\u5224\u65adres &#061;&#061; ngx.null<\/td>\n<\/tr>\n<tr>\n<td>\u70ed\u70b9Key\u6253\u7206\u5355\u5206\u7247<\/td>\n<td>\u672a\u505aL1\u672c\u5730\u7f13\u5b58<\/td>\n<td>lua_shared_dict\u62e6\u622a<\/td>\n<\/tr>\n<tr>\n<td>\u6279\u91cf\u5220\u9664\u8d85\u65f6<\/td>\n<td>\u4f7f\u7528KEYS\u800c\u975eSCAN<\/td>\n<td>\u6539\u7528SCAN\u6e38\u6807\u904d\u5386<\/td>\n<\/tr>\n<tr>\n<td>\u4e8c\u8fdb\u5236\u54cd\u5e94\u7f13\u5b58\u635f\u574f<\/td>\n<td>JSON\u5e8f\u5217\u5316\u4e8c\u8fdb\u5236\u6570\u636e<\/td>\n<td>\u6539\u7528MessagePack\u6216raw string<\/td>\n<\/tr>\n<tr>\n<td>Redis\u6545\u969c\u65f6\u5168\u7ad9500<\/td>\n<td>\u65e0\u964d\u7ea7\u903b\u8f91<\/td>\n<td>try-catch\u5305\u88f9&#xff0c;fallback\u5230\u6e90\u7ad9<\/td>\n<\/tr>\n<tr>\n<td>Cluster\u6269\u7f29\u5bb9\u5927\u91cf500<\/td>\n<td>\u672a\u5904\u7406MOVED\/ASK<\/td>\n<td>\u4f7f\u7528lua-resty-redis-cluster<\/td>\n<\/tr>\n<tr>\n<td>MGET\u8de8\u69fd\u62a5\u9519<\/td>\n<td>Key\u4e0d\u5728\u540c\u4e00\u54c8\u5e0c\u69fd<\/td>\n<td>\u62c6\u5206\u8bf7\u6c42\u6216\u5e94\u7528\u5c42\u5e76\u53d1<\/td>\n<\/tr>\n<tr>\n<td>Lua\u4ee3\u7801\u4fee\u6539\u4e0d\u751f\u6548<\/td>\n<td>lua_code_cache\u672a\u5f00\u542f<\/td>\n<td>\u786e\u8ba4lua_code_cache on;<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u6cc4\u6f0f<\/td>\n<td>Lua\u95ed\u5305\u6301\u6709\u5927\u5bf9\u8c61\u5f15\u7528<\/td>\n<td>\u53ca\u65f6\u7f6enil &#043; GC\u8c03\u4f18<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u5341\u4e00\u3001\u7ed3\u8bed<\/h3>\n<p>\u611f\u8c22\u60a8\u7684\u9605\u8bfb&#xff01;\u5982\u679c\u4f60\u6709\u4efb\u4f55\u7591\u95ee\u6216\u60f3\u8981\u5206\u4eab\u7684\u7ecf\u9a8c&#xff0c;\u8bf7\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u4ea4\u6d41&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5f15\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u662f Nginx  Redis&#xff1f;\u5728\u300aNginx\u5916\u7f6e\u7f13\u5b58\u300b\u7cfb\u5217\u524d\u6587\u4e2d&#xff0c;\u6211\u4eec\u63a2\u8ba8\u4e86\u5916\u7f6e\u7f13\u5b58\u7684\u67b6\u6784\u9009\u578b\u3001Memcached\u7684\u8f7b\u91cf\u7ea7\u573a\u666f\u4ee5\u53caerror_page\u964d\u7ea7\u4f53\u7cfb\u3002\u800c\u5728\u4f01\u4e1a\u7ea7\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;Nginx  Redis\u00a0\u7ec4\u5408\u5360\u636e\u4e86\u5916\u7f6e\u7f13\u5b5890%\u4ee5\u4e0a\u7684\u4efd\u989d\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;Redis\u4e0d\u4ec5\u662f\u4e00\u4e2aKV\u5b58\u50a8&#xff0c;\u66f4\u662f\u4e00\u4e2a\u652f\u6301\u4e30\u5bcc\u6570\u636e\u7ed3\u6784\u3001\u6301\u4e45\u5316\u3001\u96c6\u7fa4\u6a21\u5f0f\u548c\u53d1\u5e03\u8ba2\u9605\u7684\u901a\u7528\u6570\u636e\u5e73\u53f0\u3002\u4f46\u201c\u80fd\u7528\u201d\u548c\u201c\u7528\u597d\u201d\u4e4b\u95f4\u9694<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[117,105,398],"topic":[],"class_list":["post-89167","post","type-post","status-publish","format-standard","hentry","category-server","tag-nginx","tag-redis","tag-398"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/89167.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u5f15\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u662f Nginx Redis&#xff1f;\u5728\u300aNginx\u5916\u7f6e\u7f13\u5b58\u300b\u7cfb\u5217\u524d\u6587\u4e2d&#xff0c;\u6211\u4eec\u63a2\u8ba8\u4e86\u5916\u7f6e\u7f13\u5b58\u7684\u67b6\u6784\u9009\u578b\u3001Memcached\u7684\u8f7b\u91cf\u7ea7\u573a\u666f\u4ee5\u53caerror_page\u964d\u7ea7\u4f53\u7cfb\u3002\u800c\u5728\u4f01\u4e1a\u7ea7\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;Nginx Redis\u00a0\u7ec4\u5408\u5360\u636e\u4e86\u5916\u7f6e\u7f13\u5b5890%\u4ee5\u4e0a\u7684\u4efd\u989d\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;Redis\u4e0d\u4ec5\u662f\u4e00\u4e2aKV\u5b58\u50a8&#xff0c;\u66f4\u662f\u4e00\u4e2a\u652f\u6301\u4e30\u5bcc\u6570\u636e\u7ed3\u6784\u3001\u6301\u4e45\u5316\u3001\u96c6\u7fa4\u6a21\u5f0f\u548c\u53d1\u5e03\u8ba2\u9605\u7684\u901a\u7528\u6570\u636e\u5e73\u53f0\u3002\u4f46\u201c\u80fd\u7528\u201d\u548c\u201c\u7528\u597d\u201d\u4e4b\u95f4\u9694\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/89167.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-02T07:16:33+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89167.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/89167.html\",\"name\":\"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-02T07:16:33+00:00\",\"dateModified\":\"2026-08-02T07:16:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/89167.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/89167.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89167.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/89167.html","og_locale":"zh_CN","og_type":"article","og_title":"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u5f15\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u662f Nginx Redis&#xff1f;\u5728\u300aNginx\u5916\u7f6e\u7f13\u5b58\u300b\u7cfb\u5217\u524d\u6587\u4e2d&#xff0c;\u6211\u4eec\u63a2\u8ba8\u4e86\u5916\u7f6e\u7f13\u5b58\u7684\u67b6\u6784\u9009\u578b\u3001Memcached\u7684\u8f7b\u91cf\u7ea7\u573a\u666f\u4ee5\u53caerror_page\u964d\u7ea7\u4f53\u7cfb\u3002\u800c\u5728\u4f01\u4e1a\u7ea7\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;Nginx Redis\u00a0\u7ec4\u5408\u5360\u636e\u4e86\u5916\u7f6e\u7f13\u5b5890%\u4ee5\u4e0a\u7684\u4efd\u989d\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;Redis\u4e0d\u4ec5\u662f\u4e00\u4e2aKV\u5b58\u50a8&#xff0c;\u66f4\u662f\u4e00\u4e2a\u652f\u6301\u4e30\u5bcc\u6570\u636e\u7ed3\u6784\u3001\u6301\u4e45\u5316\u3001\u96c6\u7fa4\u6a21\u5f0f\u548c\u53d1\u5e03\u8ba2\u9605\u7684\u901a\u7528\u6570\u636e\u5e73\u53f0\u3002\u4f46\u201c\u80fd\u7528\u201d\u548c\u201c\u7528\u597d\u201d\u4e4b\u95f4\u9694","og_url":"https:\/\/www.wsisp.com\/helps\/89167.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-02T07:16:33+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/89167.html","url":"https:\/\/www.wsisp.com\/helps\/89167.html","name":"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-02T07:16:33+00:00","dateModified":"2026-08-02T07:16:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/89167.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/89167.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/89167.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Nginx\u5916\u7f6e\u7f13\u5b58-nginx + redis"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=89167"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89167\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=89167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=89167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=89167"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=89167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}