{"id":50034,"date":"2024-10-08T09:28:16","date_gmt":"2024-10-08T01:28:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50034\/"},"modified":"2024-10-08T09:28:16","modified_gmt":"2024-10-08T01:28:16","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e5%8a%a0%e8%bc%89%e6%99%82%e9%96%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50034\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u4f7f\u7528\u5feb\u53d6\u52a0\u901f\u52a0\u8f09\u6642\u9593"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e5%8a%a0%e8%bc%89%e6%99%82%e9%96%93-RPsJZVZITK\">Nginx \u6280\u5de7\uff1a\u4f7f\u7528\u5feb\u53d6\u52a0\u901f\u52a0\u8f09\u6642\u9593<\/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\u512a\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u914d\u7f6e\u4f7f\u5176\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528 Nginx \u7684\u5feb\u53d6\u529f\u80fd\u4f86\u52a0\u901f\u7db2\u7ad9\u7684\u52a0\u8f09\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u548c\u7db2\u7ad9\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e5%bf%ab%e5%8f%96%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-RPsJZVZITK\">\u5feb\u53d6\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5feb\u53d6\u662f\u4e00\u7a2e\u5b58\u5132\u6280\u8853\uff0c\u65e8\u5728\u6e1b\u5c11\u6578\u64da\u7684\u91cd\u8907\u8acb\u6c42\u3002\u7576\u7528\u6236\u8a2a\u554f\u7db2\u7ad9\u6642\uff0c\u4f3a\u670d\u5668\u6703\u5c07\u8acb\u6c42\u7684\u5167\u5bb9\u5b58\u5132\u5728\u5feb\u53d6\u4e2d\uff0c\u7576\u76f8\u540c\u7684\u8acb\u6c42\u518d\u6b21\u767c\u751f\u6642\uff0c\u4f3a\u670d\u5668\u53ef\u4ee5\u76f4\u63a5\u5f9e\u5feb\u53d6\u4e2d\u63d0\u4f9b\u5167\u5bb9\uff0c\u800c\u7121\u9700\u91cd\u65b0\u751f\u6210\u6216\u67e5\u8a62\u6578\u64da\u5eab\u3002\u9019\u6a23\u4e0d\u50c5\u80fd\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u9084\u80fd\u986f\u8457\u63d0\u9ad8\u52a0\u8f09\u901f\u5ea6\u3002<\/p>\n<h2 id=\"nginx-%e5%bf%ab%e5%8f%96%e7%9a%84%e9%85%8d%e7%bd%ae-RPsJZVZITK\">Nginx \u5feb\u53d6\u7684\u914d\u7f6e<\/h2>\n<p>\u8981\u5728 Nginx \u4e2d\u555f\u7528\u5feb\u53d6\uff0c\u9996\u5148\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8a2d\u7f6e\u5feb\u53d6\u5340\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>http {\n    # \u5b9a\u7fa9\u5feb\u53d6\u5340\u57df\n    proxy_cache_path \/var\/cache\/nginx 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_valid 200 1h;  # \u5c0d\u65bc 200 \u72c0\u614b\u78bc\u7684\u5feb\u53d6\u6709\u6548\u671f\u70ba 1 \u5c0f\u6642\n            proxy_cache_use_stale error timeout updating;  # \u4f7f\u7528\u904e\u671f\u5feb\u53d6\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `my_cache` \u7684\u5feb\u53d6\u5340\u57df\uff0c\u4e26\u8a2d\u7f6e\u4e86\u5feb\u53d6\u7684\u6709\u6548\u671f\u548c\u4f7f\u7528\u904e\u671f\u5feb\u53d6\u7684\u7b56\u7565\u3002\u9019\u6a23\uff0c\u7576\u7528\u6236\u8acb\u6c42 `example.com` \u6642\uff0cNginx \u5c07\u6703\u6aa2\u67e5\u5feb\u53d6\uff0c\u5982\u679c\u5feb\u53d6\u4e2d\u6709\u6709\u6548\u7684\u5167\u5bb9\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u5c07\u8acb\u6c42\u8f49\u767c\u5230\u5f8c\u7aef\u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"%e5%bf%ab%e5%8f%96%e7%ad%96%e7%95%a5%e7%9a%84%e9%81%b8%e6%93%87-RPsJZVZITK\">\u5feb\u53d6\u7b56\u7565\u7684\u9078\u64c7<\/h2>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u5feb\u53d6\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u5feb\u53d6\u7b56\u7565\uff1a<\/p>\n<ul>\n<li><strong>\u6642\u9593\u5feb\u53d6\uff1a<\/strong>\u6839\u64da\u6642\u9593\u8a2d\u7f6e\u5feb\u53d6\u7684\u6709\u6548\u671f\uff0c\u9069\u5408\u65bc\u5167\u5bb9\u4e0d\u7d93\u5e38\u8b8a\u52d5\u7684\u7db2\u7ad9\u3002<\/li>\n<li><strong>\u72c0\u614b\u78bc\u5feb\u53d6\uff1a<\/strong>\u6839\u64da HTTP \u72c0\u614b\u78bc\u4f86\u6c7a\u5b9a\u662f\u5426\u5feb\u53d6\uff0c\u901a\u5e38\u5c0d\u65bc 200 \u548c 301 \u72c0\u614b\u78bc\u9032\u884c\u5feb\u53d6\u3002<\/li>\n<li><strong>\u689d\u4ef6\u5feb\u53d6\uff1a<\/strong>\u6839\u64da\u8acb\u6c42\u7684\u7279\u5b9a\u689d\u4ef6\u4f86\u6c7a\u5b9a\u662f\u5426\u5feb\u53d6\uff0c\u4f8b\u5982\u7528\u6236\u7684\u5730\u7406\u4f4d\u7f6e\u6216\u8acb\u6c42\u7684 URL\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%bf%ab%e5%8f%96%e7%9a%84%e7%9b%a3%e6%8e%a7%e8%88%87%e7%ae%a1%e7%90%86-RPsJZVZITK\">\u5feb\u53d6\u7684\u76e3\u63a7\u8207\u7ba1\u7406<\/h2>\n<p>\u70ba\u4e86\u78ba\u4fdd\u5feb\u53d6\u7684\u6709\u6548\u6027\uff0c\u5b9a\u671f\u76e3\u63a7\u548c\u7ba1\u7406\u5feb\u53d6\u662f\u5fc5\u8981\u7684\u3002Nginx \u63d0\u4f9b\u4e86\u4e00\u4e9b\u6307\u4ee4\u4f86\u5e6b\u52a9\u7ba1\u7406\u5feb\u53d6\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>proxy_cache_purge<\/code>\uff1a\u7528\u65bc\u624b\u52d5\u6e05\u9664\u5feb\u53d6\u4e2d\u7684\u7279\u5b9a\u5167\u5bb9\u3002<\/li>\n<li><code>proxy_cache_lock<\/code>\uff1a\u9632\u6b62\u540c\u6642\u591a\u500b\u8acb\u6c42\u751f\u6210\u76f8\u540c\u7684\u5feb\u53d6\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u6307\u4ee4\u53ef\u4ee5\u5e6b\u52a9\u7cfb\u7d71\u7ba1\u7406\u54e1\u66f4\u9748\u6d3b\u5730\u63a7\u5236\u5feb\u53d6\u884c\u70ba\uff0c\u78ba\u4fdd\u7528\u6236\u7372\u5f97\u6700\u65b0\u7684\u5167\u5bb9\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-RPsJZVZITK\">\u7d50\u8ad6<\/h2>\n<p>\u901a\u904e\u5408\u7406\u914d\u7f6e Nginx \u7684\u5feb\u53d6\u529f\u80fd\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7db2\u7ad9\u7684\u52a0\u8f09\u901f\u5ea6\uff0c\u6539\u5584\u7528\u6236\u9ad4\u9a57\u3002\u5feb\u53d6\u4e0d\u50c5\u80fd\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u9084\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u6574\u9ad4\u6027\u80fd\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u7db2\u7ad9\u6548\u80fd\u7684\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u4f86\u8aaa\uff0c\u638c\u63e1 Nginx \u7684\u5feb\u53d6\u6280\u5de7\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u548c\u4f3a\u670d\u5668\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u5229\u7528\u5feb\u53d6\u6280\u8853\u986f\u8457\u52a0\u901f\u7db2\u7ad9\u52a0\u8f09\u6642\u9593\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u8207\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-50034","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50034","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=50034"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50034\/revisions"}],"predecessor-version":[{"id":50035,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50034\/revisions\/50035"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}