{"id":63564,"date":"2025-04-29T15:39:22","date_gmt":"2025-04-29T07:39:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63564\/"},"modified":"2025-04-29T15:39:22","modified_gmt":"2025-04-29T07:39:22","slug":"nginx%e6%80%8e%e4%b9%88%e8%b7%b3%e8%bd%ac-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63564\/","title":{"rendered":"nginx\u600e\u4e48\u8df3\u8f6c"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 nginx \u8fdb\u884c\u91cd\u5b9a\u5411\u53ef\u5c06\u8bf7\u6c42\u8def\u7531\u5230\u4e0d\u540c url\u3002\u5177\u4f53\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u914d\u7f6e\u670d\u52a1\u5668\u5757\uff0c\u6307\u5b9a\u76d1\u542c\u7aef\u53e3\u548c\u670d\u52a1\u5668\u540d\u79f0\u3002\u4f7f\u7528 rewrite \u6307\u4ee4\u6307\u5b9a\u91cd\u5b9a\u5411\u7c7b\u578b\uff0c\u5982\u6c38\u4e45\u91cd\u5b9a\u5411 (301) \u6216\u4e34\u65f6\u91cd\u5b9a\u5411 (302)\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8bf7\u6c42\uff0c\u5e76\u6307\u793a\u91cd\u5b9a\u5411\u7684 url\u3002\u901a\u8fc7\u6d4f\u89c8\u5668\u6216 curl \u547d\u4ee4\u6d4b\u8bd5\u91cd\u5b9a\u5411\uff0c\u68c0\u67e5\u54cd\u5e94\u4ee3\u7801\u662f\u5426\u4e0e\u9884\u671f\u4e00\u81f4\uff0c\u8bf7\u6c42\u662f\u5426\u6b63\u786e\u91cd\u5b9a\u5411\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/27\/2024052720512066681.jpg\" class=\"aligncenter\" title=\"nginx\u600e\u4e48\u8df3\u8f6c\u63d2\u56fe\" alt=\"nginx\u600e\u4e48\u8df3\u8f6c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Nginx \u8fdb\u884c\u91cd\u5b9a\u5411<\/strong><\/p>\n<p><strong>\u5f00\u95e8\u89c1\u5c71\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Nginx \u8fdb\u884c\u91cd\u5b9a\u5411\u53ef\u4ee5\u5c06\u8bf7\u6c42\u4ece\u4e00\u4e2a URL \u8def\u7531\u5230\u53e6\u4e00\u4e2a URL\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6e\u670d\u52a1\u5668\u5757\uff1a<\/strong><\/p>\n<pre>server {\n listen 80;\n server_name example.com;\n\n # \u91cd\u5b9a\u5411\u5230 www.example.com\n rewrite ^ https:\/\/www.example.com$request_uri? permanent;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u6307\u5b9a\u91cd\u5b9a\u5411\u7c7b\u578b\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>301 \u6c38\u4e45\u91cd\u5b9a\u5411\uff1a<\/strong> \u5c06\u8bf7\u6c42\u6c38\u4e45\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\uff0c\u6d4f\u89c8\u5668\u4f1a\u66f4\u65b0\u4e66\u7b7e\u548c\u94fe\u63a5\u3002<\/li>\n<li> <strong>302 \u4e34\u65f6\u91cd\u5b9a\u5411\uff1a<\/strong> \u5c06\u8bf7\u6c42\u6682\u65f6\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\uff0c\u6d4f\u89c8\u5668\u4e0d\u4f1a\u66f4\u65b0\u4e66\u7b7e\u548c\u94fe\u63a5\u3002<\/li>\n<li> <strong>303 \u67e5\u770b\u5176\u4ed6\u4f4d\u7f6e\uff1a<\/strong> \u6307\u793a\u6d4f\u89c8\u5668\u4ece\u4e0d\u540c\u7684\u4f4d\u7f6e\u83b7\u53d6\u8bf7\u6c42\u7684\u54cd\u5e94\u3002<\/li>\n<li> <strong>307 \u4e34\u65f6\u91cd\u5b9a\u5411\uff1a<\/strong> \u4e0e 302 \u7c7b\u4f3c\uff0c\u4f46\u9002\u7528\u4e8e HTTP POST \u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/strong><\/p>\n<ul>\n<li>^\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u3002<\/li>\n<li>$\uff1a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002<\/li>\n<li>?\uff1a\u8868\u793a\u67e5\u8be2\u5b57\u7b26\u4e32\u662f\u53ef\u9009\u7684\u3002<\/li>\n<li>permanent: \u6307\u793a\u91cd\u5b9a\u5411\u662f\u6c38\u4e45\u6027\u7684\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5\u91cd\u5b9a\u5411\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u6d4f\u89c8\u5668\u6216 curl \u547d\u4ee4\u8bbf\u95ee\u539f\u59cb URL\u3002<\/li>\n<li>\u68c0\u67e5\u54cd\u5e94\u4ee3\u7801\u662f\u5426\u4e0e\u9884\u671f\u7684\u4e00\u81f4\u3002<\/li>\n<li>\u786e\u8ba4\u8bf7\u6c42\u662f\u5426\u88ab\u6b63\u786e\u91cd\u5b9a\u5411\u5230\u65b0 URL\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u5176\u4ed6\u91cd\u5b9a\u5411\u9009\u9879\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>location \u5757\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528 location \u5757\u57fa\u4e8e\u8def\u5f84\u6216\u5176\u4ed6\u6761\u4ef6\u8fdb\u884c\u91cd\u5b9a\u5411\u3002<\/li>\n<li> <strong>proxy_pass\uff1a<\/strong> \u53ef\u4ee5\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u53e6\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u5e76\u9009\u62e9\u6027\u5730\u4f7f\u7528 return \u6307\u4ee4\u8fdb\u884c\u91cd\u5b9a\u5411\u3002<\/li>\n<li> <strong>if \u6761\u4ef6\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528 if \u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u8bf7\u6c42\u6761\u4ef6\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u91cd\u5b9a\u5411\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u8df3\u8f6c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 nginx \u8fdb\u884c\u91cd\u5b9a\u5411\u53ef\u5c06\u8bf7\u6c42\u8def\u7531\u5230\u4e0d\u540c url\u3002\u5177\u4f53\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u914d\u7f6e\u670d\u52a1\u5668\u5757\uff0c\u6307\u5b9a\u76d1\u542c\u7aef\u53e3\u548c\u670d\u52a1\u5668\u540d\u79f0\u3002\u4f7f\u7528 rewrite \u6307\u4ee4\u6307\u5b9a\u91cd\u5b9a\u5411\u7c7b\u578b\uff0c\u5982\u6c38\u4e45\u91cd\u5b9a\u5411 (301) \u6216\u4e34\u65f6\u91cd\u5b9a\u5411 (302)\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8bf7\u6c42\uff0c\u5e76\u6307\u793a\u91cd\u5b9a\u5411\u7684 url\u3002\u901a\u8fc7\u6d4f\u89c8\u5668\u6216 curl \u547d\u4ee4\u6d4b\u8bd5\u91cd\u5b9a\u5411\uff0c\u68c0\u67e5\u54cd\u5e94\u4ee3\u7801\u662f\u5426\u4e0e\u9884\u671f\u4e00\u81f4\uff0c\u8bf7\u6c42\u662f\u5426\u6b63\u786e\u91cd\u5b9a\u5411\u3002 \u5982\u4f55\u4f7f\u7528 Nginx \u8fdb\u884c\u91cd\u5b9a\u5411 \u5f00\u95e8\u89c1\u5c71\uff1a \u4f7f\u7528 Nginx \u8fdb\u884c\u91cd\u5b9a\u5411\u53ef\u4ee5\u5c06\u8bf7\u6c42\u4ece\u4e00\u4e2a URL \u8def\u7531\u5230\u53e6\u4e00\u4e2a URL\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u914d\u7f6e\u670d\u52a1\u5668\u5757\uff1a server { listen 80; server_name example.com; # \u91cd\u5b9a\u5411\u5230 www.example.com rewrite ^ https:\/\/www.example.com$request_uri? permanent; } \u767b\u5f55\u540e\u590d\u5236 \u6307\u5b9a\u91cd\u5b9a\u5411\u7c7b\u578b\uff1a 301 \u6c38\u4e45\u91cd\u5b9a\u5411\uff1a \u5c06\u8bf7\u6c42\u6c38\u4e45\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\uff0c\u6d4f\u89c8\u5668\u4f1a\u66f4\u65b0\u4e66\u7b7e\u548c\u94fe\u63a5\u3002 302 \u4e34\u65f6\u91cd\u5b9a\u5411\uff1a \u5c06\u8bf7\u6c42\u6682\u65f6\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\uff0c\u6d4f\u89c8\u5668\u4e0d\u4f1a\u66f4\u65b0\u4e66\u7b7e\u548c\u94fe\u63a5\u3002 303 \u67e5\u770b\u5176\u4ed6\u4f4d\u7f6e\uff1a \u6307\u793a\u6d4f\u89c8\u5668\u4ece\u4e0d\u540c\u7684\u4f4d\u7f6e\u83b7\u53d6\u8bf7\u6c42\u7684\u54cd\u5e94\u3002 307 \u4e34\u65f6\u91cd\u5b9a\u5411\uff1a \u4e0e 302 \u7c7b\u4f3c\uff0c\u4f46\u9002\u7528\u4e8e HTTP [&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-63564","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63564","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=63564"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63564\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}