{"id":49896,"date":"2024-10-08T08:24:13","date_gmt":"2024-10-08T00:24:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49896\/"},"modified":"2024-10-08T08:24:13","modified_gmt":"2024-10-08T00:24:13","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-proxy_cache_bypass-%e6%8c%87%e4%bb%a4%e9%80%b2%e8%a1%8c%e6%a2%9d%e4%bb%b6%e5%bf%ab%e5%8f%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49896\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u4f7f\u7528 proxy_cache_bypass \u6307\u4ee4\u9032\u884c\u689d\u4ef6\u5feb\u53d6"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-proxy_cache_bypass-%e6%8c%87%e4%bb%a4%e9%80%b2%e8%a1%8c%e6%a2%9d%e4%bb%b6%e5%bf%ab%e5%8f%96-uDMVsgJLFK\">Nginx \u6280\u5de7\uff1a\u4f7f\u7528 proxy_cache_bypass \u6307\u4ee4\u9032\u884c\u689d\u4ef6\u5feb\u53d6<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u8ca0\u8f09\u5e73\u8861\u3001\u53cd\u5411\u4ee3\u7406\u548c\u5feb\u53d6\u7b49\u5834\u666f\u3002\u5728\u8a31\u591a\u60c5\u6cc1\u4e0b\uff0c\u5feb\u53d6\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7db2\u7ad9\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u7136\u800c\uff0c\u6709\u6642\u5019\u6211\u5011\u9700\u8981\u6839\u64da\u7279\u5b9a\u689d\u4ef6\u4f86\u63a7\u5236\u5feb\u53d6\u7684\u884c\u70ba\uff0c\u9019\u6642\u5019 <code>proxy_cache_bypass<\/code> \u6307\u4ee4\u5c31\u6d3e\u4e0a\u7528\u5834\u4e86\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-proxy_cache_bypass%ef%bc%9f-uDMVsgJLFK\">\u4ec0\u9ebc\u662f proxy_cache_bypass\uff1f<\/h2>\n<p><code>proxy_cache_bypass<\/code> \u662f Nginx \u4e2d\u7684\u4e00\u500b\u6307\u4ee4\uff0c\u7528\u65bc\u6c7a\u5b9a\u5728\u7279\u5b9a\u689d\u4ef6\u4e0b\u662f\u5426\u7e5e\u904e\u5feb\u53d6\u3002\u7576\u9019\u500b\u6307\u4ee4\u7684\u689d\u4ef6\u6210\u7acb\u6642\uff0cNginx \u5c07\u4e0d\u6703\u5f9e\u5feb\u53d6\u4e2d\u63d0\u4f9b\u5167\u5bb9\uff0c\u800c\u662f\u76f4\u63a5\u5411\u5f8c\u7aef\u4f3a\u670d\u5668\u8acb\u6c42\u6700\u65b0\u7684\u8cc7\u6599\u3002\u9019\u5c0d\u65bc\u9700\u8981\u5373\u6642\u66f4\u65b0\u7684\u5167\u5bb9\u6216\u7279\u5b9a\u7528\u6236\u8acb\u6c42\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%a0%b4%e6%99%af-uDMVsgJLFK\">\u4f7f\u7528\u5834\u666f<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u5834\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u7528\u6236\u767b\u9304\u5f8c\u7684\u500b\u6027\u5316\u5167\u5bb9\uff1a<\/strong>\u7576\u7528\u6236\u767b\u9304\u5f8c\uff0c\u4ed6\u5011\u53ef\u80fd\u6703\u770b\u5230\u500b\u6027\u5316\u7684\u5167\u5bb9\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u70ba\u4e86\u78ba\u4fdd\u7528\u6236\u7372\u5f97\u6700\u65b0\u7684\u8cc7\u6599\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>proxy_cache_bypass<\/code>\u3002<\/li>\n<li><strong>\u7ba1\u7406\u5f8c\u53f0\u7684\u5373\u6642\u6578\u64da\uff1a<\/strong>\u5c0d\u65bc\u7ba1\u7406\u5f8c\u53f0\u7684\u8acb\u6c42\uff0c\u901a\u5e38\u9700\u8981\u5373\u6642\u6578\u64da\uff0c\u56e0\u6b64\u53ef\u4ee5\u8a2d\u7f6e\u689d\u4ef6\u4f86\u7e5e\u904e\u5feb\u53d6\u3002<\/li>\n<li><strong>\u7279\u5b9a\u8acb\u6c42\u7684\u5373\u6642\u97ff\u61c9\uff1a<\/strong>\u67d0\u4e9b API \u8acb\u6c42\u53ef\u80fd\u9700\u8981\u5373\u6642\u97ff\u61c9\uff0c\u9019\u6642\u5019\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b64\u6307\u4ee4\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae-proxy_cache_bypass-uDMVsgJLFK\">\u5982\u4f55\u914d\u7f6e proxy_cache_bypass<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u914d\u7f6e\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <code>proxy_cache_bypass<\/code> \u6307\u4ee4\uff1a<\/p>\n<pre><code>http {\n    proxy_cache_path \/path\/to\/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/ {\n            proxy_pass http:\/\/backend_server;\n            proxy_cache my_cache;\n\n            # \u6839\u64da\u7279\u5b9a\u689d\u4ef6\u7e5e\u904e\u5feb\u53d6\n            set $bypass_cache 0;\n            if ($http_cookie ~* \"session_id\") {\n                set $bypass_cache 1;\n            }\n            proxy_cache_bypass $bypass_cache;\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u5feb\u53d6\u8def\u5f91\u548c\u5feb\u53d6\u5340\u57df\u3002\u7136\u5f8c\u5728 <code>location<\/code> \u5340\u584a\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>proxy_pass<\/code> \u5c07\u8acb\u6c42\u8f49\u767c\u5230\u5f8c\u7aef\u4f3a\u670d\u5668\u3002\u63a5\u8457\uff0c\u6211\u5011\u8a2d\u7f6e\u4e86\u4e00\u500b\u8b8a\u6578 <code>$bypass_cache<\/code>\uff0c\u6839\u64da\u8acb\u6c42\u4e2d\u7684 cookie \u5224\u65b7\u662f\u5426\u9700\u8981\u7e5e\u904e\u5feb\u53d6\u3002\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u9019\u500b\u8b8a\u6578\u50b3\u905e\u7d66 <code>proxy_cache_bypass<\/code> \u6307\u4ee4\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-uDMVsgJLFK\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 <code>proxy_cache_bypass<\/code> \u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u904e\u5ea6\u4f7f\u7528\u7e5e\u904e\u5feb\u53d6\u53ef\u80fd\u6703\u5c0e\u81f4\u4f3a\u670d\u5668\u8ca0\u64d4\u589e\u52a0\uff0c\u56e0\u6b64\u61c9\u8b39\u614e\u8a2d\u7f6e\u689d\u4ef6\u3002<\/li>\n<li>\u78ba\u4fdd\u5feb\u53d6\u7684\u6709\u6548\u6027\u548c\u4e00\u81f4\u6027\uff0c\u907f\u514d\u7528\u6236\u7372\u5f97\u904e\u671f\u7684\u8cc7\u6599\u3002<\/li>\n<li>\u6e2c\u8a66\u914d\u7f6e\u4ee5\u78ba\u4fdd\u5176\u6309\u9810\u671f\u5de5\u4f5c\uff0c\u4e26\u76e3\u63a7\u4f3a\u670d\u5668\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-uDMVsgJLFK\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 <code>proxy_cache_bypass<\/code> \u6307\u4ee4\u53ef\u4ee5\u9748\u6d3b\u5730\u63a7\u5236 Nginx \u7684\u5feb\u53d6\u884c\u70ba\uff0c\u6839\u64da\u7279\u5b9a\u689d\u4ef6\u4f86\u6c7a\u5b9a\u662f\u5426\u7e5e\u904e\u5feb\u53d6\u3002\u9019\u5c0d\u65bc\u9700\u8981\u5373\u6642\u66f4\u65b0\u7684\u5167\u5bb9\u6216\u7279\u5b9a\u7528\u6236\u8acb\u6c42\u975e\u5e38\u6709\u7528\u3002\u900f\u904e\u5408\u7406\u7684\u914d\u7f6e\uff0c\u60a8\u53ef\u4ee5\u5728\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u7684\u540c\u6642\uff0c\u78ba\u4fdd\u7528\u6236\u7372\u5f97\u6700\u65b0\u7684\u8cc7\u6599\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u7684 proxy_cache_bypass \u6307\u4ee4\uff0c\u5b78\u7fd2\u5982\u4f55\u6839\u64da\u689d\u4ef6\u6709\u6548\u7ba1\u7406\u5feb\u53d6\uff0c\u63d0\u9ad8\u7db2\u7ad9\u6027\u80fd\u8207\u9748\u6d3b\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-49896","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=49896"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49896\/revisions"}],"predecessor-version":[{"id":49897,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49896\/revisions\/49897"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}