{"id":16000,"date":"2024-11-18T11:43:25","date_gmt":"2024-11-18T03:43:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16000\/"},"modified":"2024-11-18T11:43:25","modified_gmt":"2024-11-18T03:43:25","slug":"nginx%e7%bc%93%e5%ad%98%e6%b8%85%e7%90%86%e9%85%8d%e7%bd%ae%ef%bc%8c%e6%9b%b4%e6%96%b0%e7%bd%91%e7%ab%99%e9%9d%99%e6%80%81%e8%b5%84%e6%ba%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16000\/","title":{"rendered":"Nginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e\uff0c\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90"},"content":{"rendered":"<p>nginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e\uff0c\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u7f51\u7ad9\u7684\u53d1\u5c55\u548c\u5185\u5bb9\u7684\u66f4\u65b0\uff0c\u4e3a\u63d0\u9ad8\u7f51\u7ad9\u7684\u8bbf\u95ee\u901f\u5ea6\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u5f88\u591a\u7f51\u7ad9\u90fd\u4f1a\u4f7f\u7528Nginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u3002Nginx\u7684\u7f13\u5b58\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u7f51\u7ad9\u7684\u6027\u80fd\uff0c\u7136\u800c\u5728\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u6e05\u7406Nginx\u7f13\u5b58\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u914d\u7f6eNginx\u8fdb\u884c\u7f13\u5b58\u6e05\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u52a8\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u3002<\/p>\n<p>\u4e00\u3001Nginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u7f13\u5b58\u6e05\u7406\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u6bd4\u5982\/cache\/clear\u3002\u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p>location \/cache\/clear {<\/p>\n<pre> allow 127.0.0.1;\n deny all;\n proxy_cache_purge CACHE_NAME \"$scheme$request_method$host$request_uri\";\n return 204;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>}<\/p>\n<\/li>\n<li>\n<p>\u4fee\u6539Nginx\u7f13\u5b58\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u7f13\u5b58\u533a\u5757\uff0c\u5e76\u547d\u540d\u4e3aCACHE_NAME\u3002\u5728\u8fd9\u4e2a\u7f13\u5b58\u533a\u5757\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p>proxy_cache_path \/path\/to\/cache levels=1:2 keys_zone=CACHE_NAME:10m max_size=10g inactive=60m use_temp_path=off;<\/p>\n<p>proxy_cache_key &#8220;$scheme$request_method$host$request_uri&#8221;;<\/p>\n<\/li>\n<li>\u91cd\u542fNginx\u670d\u52a1\u5668\uff0c\u4f7f\u914d\u7f6e\u6587\u4ef6\u751f\u6548\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90<\/p>\n<ol>\n<li>\u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u5b58\u653e\u9759\u6001\u8d44\u6e90\u7684\u76ee\u5f55\uff0c\u6bd4\u5982\/static\u3002<\/li>\n<li>\n<p>\u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u9759\u6001\u8d44\u6e90\u7684location\u3002<\/p>\n<p>location \/static {<\/p>\n<pre> root \/path\/to\/static;\n expires max;\n add_header Cache-Control public;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>}<\/p>\n<\/li>\n<li>\u914d\u7f6e\u4e00\u4e2a\u811a\u672c\u6765\u66f4\u65b0\u7f51\u7ad9\u7684\u9759\u6001\u8d44\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528shell\u811a\u672c\u6216\u8005\u5176\u4ed6\u811a\u672c\u8bed\u8a00\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u7684\u811a\u672c\uff1a<\/li>\n<\/ol>\n<pre>#!\/bin\/bash\n\n# \u9759\u6001\u8d44\u6e90\u76ee\u5f55\nSTATIC_DIR=\"\/path\/to\/static\"\n# \u7f13\u5b58\u6e05\u7406URL\nCACHE_CLEAR_URL=\"http:\/\/localhost\/cache\/clear\"\n\n# \u8fdb\u5165\u9759\u6001\u8d44\u6e90\u76ee\u5f55\ncd $STATIC_DIR\n\n# \u62c9\u53d6\u6700\u65b0\u7684\u4ee3\u7801\ngit pull\n\n# \u6e05\u7406Nginx\u7f13\u5b58\ncurl -X PURGE $CACHE_CLEAR_URL\n\n# \u590d\u5236\u9759\u6001\u8d44\u6e90\u5230Nginx\u7684\u76ee\u5f55\ncp -R .\/* \/path\/to\/nginx\/static\n\n# \u91cd\u542fNginx\u670d\u52a1\u5668\nservice nginx restart<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u7684\u811a\u672c\u4f1a\u5148\u5207\u6362\u5230\u9759\u6001\u8d44\u6e90\u76ee\u5f55\uff0c\u7136\u540e\u901a\u8fc7git\u547d\u4ee4\u62c9\u53d6\u6700\u65b0\u7684\u4ee3\u7801\u3002\u63a5\u7740\uff0c\u5b83\u4f1a\u4f7f\u7528curl\u53d1\u9001\u7f13\u5b58\u6e05\u7406\u8bf7\u6c42\uff0c\u5c06Nginx\u7684\u7f13\u5b58\u6e05\u7a7a\u3002\u7136\u540e\uff0c\u5b83\u5c06\u65b0\u7684\u9759\u6001\u8d44\u6e90\u590d\u5236\u5230Nginx\u7684\u76ee\u5f55\uff0c\u5e76\u6700\u540e\u91cd\u542fNginx\u670d\u52a1\u5668\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u6216\u5176\u4ed6\u65b9\u5f0f\u6765\u81ea\u52a8\u6267\u884c\u66f4\u65b0\u9759\u6001\u8d44\u6e90\u7684\u811a\u672c\u3002\u6bd4\u5982\u53ef\u4ee5\u6bcf\u5c0f\u65f6\u6267\u884c\u4e00\u6b21\uff0c\u786e\u4fdd\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u65f6\u523b\u4fdd\u6301\u6700\u65b0\u3002<\/li>\n<\/ol>\n<p>\u7ed3\u8bba\uff1a<br \/>\u901a\u8fc7\u914d\u7f6eNginx\u7684\u7f13\u5b58\u6e05\u7406\u548c\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u7684\u811a\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u4fdd\u6301\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u5185\u5bb9\u66f4\u65b0\u3002\u5728\u7f51\u7ad9\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u914d\u7f6e\u548c\u4f18\u5316\uff0c\u4ee5\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u548c\u7f51\u7ad9\u7684\u8bbf\u95ee\u901f\u5ea6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fNginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e\uff0c\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e\uff0c\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90 \u5f15\u8a00\uff1a\u968f\u7740\u7f51\u7ad9\u7684\u53d1\u5c55\u548c\u5185\u5bb9\u7684\u66f4\u65b0\uff0c\u4e3a\u63d0\u9ad8\u7f51\u7ad9\u7684\u8bbf\u95ee\u901f\u5ea6\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u5f88\u591a\u7f51\u7ad9\u90fd\u4f1a\u4f7f\u7528Nginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u3002Nginx\u7684\u7f13\u5b58\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u7f51\u7ad9\u7684\u6027\u80fd\uff0c\u7136\u800c\u5728\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u6e05\u7406Nginx\u7f13\u5b58\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u914d\u7f6eNginx\u8fdb\u884c\u7f13\u5b58\u6e05\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u52a8\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90\u3002 \u4e00\u3001Nginx\u7f13\u5b58\u6e05\u7406\u914d\u7f6e \u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u63a5\u6536\u7f13\u5b58\u6e05\u7406\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u6bd4\u5982\/cache\/clear\u3002\u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a location \/cache\/clear { allow 127.0.0.1; deny all; proxy_cache_purge CACHE_NAME &#8220;$scheme$request_method$host$request_uri&#8221;; return 204; \u767b\u5f55\u540e\u590d\u5236 } \u4fee\u6539Nginx\u7f13\u5b58\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u7f13\u5b58\u533a\u5757\uff0c\u5e76\u547d\u540d\u4e3aCACHE_NAME\u3002\u5728\u8fd9\u4e2a\u7f13\u5b58\u533a\u5757\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a proxy_cache_path \/path\/to\/cache levels=1:2 keys_zone=CACHE_NAME:10m max_size=10g inactive=60m use_temp_path=off; proxy_cache_key &#8220;$scheme$request_method$host$request_uri&#8221;; \u91cd\u542fNginx\u670d\u52a1\u5668\uff0c\u4f7f\u914d\u7f6e\u6587\u4ef6\u751f\u6548\u3002 \u4e8c\u3001\u66f4\u65b0\u7f51\u7ad9\u9759\u6001\u8d44\u6e90 \u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2a\u5b58\u653e\u9759\u6001\u8d44\u6e90\u7684\u76ee\u5f55\uff0c\u6bd4\u5982\/static\u3002 \u5728Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u9759\u6001\u8d44\u6e90\u7684location\u3002 location \/static { root \/path\/to\/static; expires max; add_header Cache-Control public; \u767b\u5f55\u540e\u590d\u5236 } \u914d\u7f6e\u4e00\u4e2a\u811a\u672c\u6765\u66f4\u65b0\u7f51\u7ad9\u7684\u9759\u6001\u8d44\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528shell\u811a\u672c\u6216\u8005\u5176\u4ed6\u811a\u672c\u8bed\u8a00\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u7684\u811a\u672c\uff1a #!\/bin\/bash # \u9759\u6001\u8d44\u6e90\u76ee\u5f55 STATIC_DIR=&#8221;\/path\/to\/static&#8221; # \u7f13\u5b58\u6e05\u7406URL CACHE_CLEAR_URL=&#8221;http:\/\/localhost\/cache\/clear&#8221; # \u8fdb\u5165\u9759\u6001\u8d44\u6e90\u76ee\u5f55 cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-16000","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=16000"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16000\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}