{"id":49834,"date":"2024-10-08T07:53:18","date_gmt":"2024-10-07T23:53:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49834\/"},"modified":"2024-10-08T07:53:18","modified_gmt":"2024-10-07T23:53:18","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-proxy_cache_background_update-%e9%80%b2%e8%a1%8c%e5%be%8c%e5%8f%b0%e6%9b%b4%e6%96%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49834\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u4f7f\u7528 proxy_cache_background_update \u9032\u884c\u5f8c\u53f0\u66f4\u65b0"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-proxy_cache_background_update-%e9%80%b2%e8%a1%8c%e5%be%8c%e5%8f%b0%e6%9b%b4%e6%96%b0-EktpsDxRpL\">Nginx \u6280\u5de7\uff1a\u4f7f\u7528 proxy_cache_background_update \u9032\u884c\u5f8c\u53f0\u66f4\u65b0<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5176\u5f37\u5927\u7684\u53cd\u5411\u4ee3\u7406\u529f\u80fd\u548c\u5feb\u53d6\u6a5f\u5236\u4f7f\u5f97 Nginx \u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u7684\u9996\u9078\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e Nginx \u7684\u4e00\u500b\u91cd\u8981\u529f\u80fd\uff1a<code>proxy_cache_background_update<\/code>\uff0c\u4e26\u4e86\u89e3\u5982\u4f55\u5229\u7528\u9019\u500b\u529f\u80fd\u9032\u884c\u5f8c\u53f0\u66f4\u65b0\uff0c\u4ee5\u63d0\u5347\u7db2\u7ad9\u7684\u6548\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-proxy_cache_background_update%ef%bc%9f-EktpsDxRpL\">\u4ec0\u9ebc\u662f proxy_cache_background_update\uff1f<\/h2>\n<p><code>proxy_cache_background_update<\/code> \u662f Nginx \u4e2d\u7684\u4e00\u500b\u6307\u4ee4\uff0c\u4e3b\u8981\u7528\u65bc\u5728\u5feb\u53d6\u904e\u671f\u5f8c\uff0c\u5141\u8a31 Nginx \u5728\u80cc\u666f\u4e2d\u66f4\u65b0\u5feb\u53d6\u3002\u9019\u610f\u5473\u8457\u7576\u7528\u6236\u8acb\u6c42\u4e00\u500b\u5df2\u7d93\u904e\u671f\u7684\u5feb\u53d6\u5167\u5bb9\u6642\uff0cNginx \u4e0d\u6703\u7acb\u5373\u8fd4\u56de 404 \u932f\u8aa4\u6216\u820a\u7684\u5feb\u53d6\u5167\u5bb9\uff0c\u800c\u662f\u6703\u5728\u80cc\u666f\u4e2d\u5411\u4e0a\u6e38\u4f3a\u670d\u5668\u767c\u9001\u8acb\u6c42\u4ee5\u7372\u53d6\u6700\u65b0\u7684\u5167\u5bb9\uff0c\u4e26\u5728\u66f4\u65b0\u5b8c\u6210\u5f8c\u5c07\u65b0\u5167\u5bb9\u5b58\u5165\u5feb\u53d6\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8-proxy_cache_background_update%ef%bc%9f-EktpsDxRpL\">\u70ba\u4ec0\u9ebc\u4f7f\u7528 proxy_cache_background_update\uff1f<\/h2>\n<p>\u4f7f\u7528 <code>proxy_cache_background_update<\/code> \u6709\u5e7e\u500b\u660e\u986f\u7684\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u5347\u7528\u6236\u9ad4\u9a57\uff1a<\/strong>\u7528\u6236\u5728\u8acb\u6c42\u904e\u671f\u5167\u5bb9\u6642\uff0c\u4e0d\u6703\u611f\u53d7\u5230\u5ef6\u9072\uff0c\u56e0\u70ba Nginx \u6703\u7acb\u5373\u8fd4\u56de\u820a\u7684\u5feb\u53d6\u5167\u5bb9\uff0c\u4e26\u5728\u80cc\u666f\u4e2d\u66f4\u65b0\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u4f3a\u670d\u5668\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u5728\u80cc\u666f\u4e2d\u66f4\u65b0\u5feb\u53d6\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u4e0a\u6e38\u4f3a\u670d\u5668\u7684\u8acb\u6c42\u983b\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u60c5\u6cc1\u4e0b\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u5feb\u53d6\u547d\u4e2d\u7387\uff1a<\/strong>\u5373\u4f7f\u5728\u5feb\u53d6\u904e\u671f\u7684\u60c5\u6cc1\u4e0b\uff0c\u4ecd\u7136\u80fd\u5920\u63d0\u4f9b\u820a\u7684\u5feb\u53d6\u5167\u5bb9\uff0c\u5f9e\u800c\u63d0\u9ad8\u5feb\u53d6\u7684\u547d\u4e2d\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae-proxy_cache_background_update-EktpsDxRpL\">\u5982\u4f55\u914d\u7f6e proxy_cache_background_update<\/h2>\n<p>\u8981\u4f7f\u7528 <code>proxy_cache_background_update<\/code>\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u555f\u7528\u4e86\u5feb\u53d6\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u914d\u7f6e\u7bc4\u4f8b\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            proxy_cache_background_update on;  # \u555f\u7528\u5f8c\u53f0\u66f4\u65b0\n            proxy_cache_valid 200 1h;          # \u8a2d\u5b9a\u5feb\u53d6\u6709\u6548\u671f\n            proxy_cache_use_stale error timeout updating;  # \u4f7f\u7528\u820a\u5feb\u53d6\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\uff0c\u7136\u5f8c\u5728\u4f3a\u670d\u5668\u5340\u584a\u4e2d\u914d\u7f6e\u4e86\u53cd\u5411\u4ee3\u7406\u548c\u5feb\u53d6\u3002\u7279\u5225\u6ce8\u610f\u7684\u662f\uff0c<code>proxy_cache_background_update on;<\/code> \u6307\u4ee4\u7684\u4f7f\u7528\uff0c\u9019\u5c07\u555f\u7528\u5f8c\u53f0\u66f4\u65b0\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-EktpsDxRpL\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u96d6\u7136 <code>proxy_cache_background_update<\/code> \u63d0\u4f9b\u4e86\u8a31\u591a\u512a\u52e2\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u4e5f\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u4e0a\u6e38\u4f3a\u670d\u5668\u7684\u6027\u80fd\u8db3\u5920\uff0c\u56e0\u70ba\u5728\u9ad8\u6d41\u91cf\u7684\u60c5\u6cc1\u4e0b\uff0c\u80cc\u666f\u66f4\u65b0\u53ef\u80fd\u6703\u589e\u52a0\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002<\/li>\n<li>\u5408\u7406\u8a2d\u5b9a\u5feb\u53d6\u7684\u6709\u6548\u671f\uff0c\u4ee5\u907f\u514d\u904e\u671f\u5167\u5bb9\u7684\u983b\u7e41\u8acb\u6c42\u3002<\/li>\n<li>\u76e3\u63a7\u5feb\u53d6\u7684\u547d\u4e2d\u7387\u548c\u4f3a\u670d\u5668\u7684\u6027\u80fd\uff0c\u4ee5\u4fbf\u53ca\u6642\u8abf\u6574\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EktpsDxRpL\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 Nginx \u7684 <code>proxy_cache_background_update<\/code> \u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u7db2\u7ad9\u7684\u6548\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002\u901a\u904e\u5728\u80cc\u666f\u4e2d\u66f4\u65b0\u5feb\u53d6\uff0c\u7db2\u7ad9\u80fd\u5920\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4fdd\u6301\u7a69\u5b9a\u6027\uff0c\u4e26\u6e1b\u5c11\u5c0d\u4e0a\u6e38\u4f3a\u670d\u5668\u7684\u8acb\u6c42\u8ca0\u64d4\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u7684\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u6280\u5de7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u80fd\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\u7a0b\u5f0f\u90e8\u7f72\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Nginx \u7684 proxy_cache_background_update \u6280\u5de7\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u5f8c\u53f0\u7de9\u5b58\u66f4\u65b0\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\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-49834","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49834","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=49834"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49834\/revisions"}],"predecessor-version":[{"id":49835,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49834\/revisions\/49835"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}