{"id":177725,"date":"2020-05-06T11:05:23","date_gmt":"2020-05-06T03:05:23","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=177725"},"modified":"2020-04-24T10:30:16","modified_gmt":"2020-04-24T02:30:16","slug":"nginx-301-https","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-301-https.html","title":{"rendered":"Nginx\u5b9e\u73b0301\u8df3\u8f6c\u81f3https\u7684\u6839\u57df\u540d\u5c55\u793a"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u4f7f\u7528Nginx\u5b9e\u73b0301\u8df3\u8f6c\u81f3https\u7684\u6839\u57df\u540d\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u968f\u7740\u5c0f\u7f16\u6765\u4e00\u8d77\u5b66\u4e60\u5b66\u4e60\u5427\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u57fa\u4e8e SEO \u548c\u5b89\u5168\u6027\u7684\u8003\u91cf\uff0c\u9700\u8981\u8fdb\u884c 301 \u8df3\u8f6c\uff0c\u4ee5\u4e0b\u4f7f\u7528 Nginx \u4f5c\u901a\u7528\u5904\u7406<\/p>\n

\u5b9e\u73b0\u7ed3\u679c<\/strong><\/div>\n

\u9700\u8981\u5c06\u4ee5\u4e0b\u5730\u5740\u90fd\u7edf\u4e00\u8df3\u8f6c\u5230 https \u7684\u6839\u57df\u540d <\/p>\n

\r\nhttps:\/\/chanvinxiao.com\r\nhttp:\/\/chanvinxiao.com \uff08\u4e0d\u5e26 www \u7684 http\uff09\r\nhttp:\/\/www.chanvinxiao.com \uff08\u5e26 www \u7684 http\uff09\r\nhttps:\/\/www.chanvinxiao.com \uff08\u5e26 www \u7684 https\uff09\r\n<\/pre>\n
301 \u4e0e 302 \u7684\u533a\u522b<\/strong><\/div>\n

301 \u662f\u6c38\u4e45\u91cd\u5b9a\u5411\uff0c302 \u662f\u4e34\u65f6\u8df3\u8f6c\uff0c\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\u641c\u7d22\u5f15\u64ce\u5bf9\u6b64\u7684\u5bf9\u5f85\u65b9\u5f0f<\/p>\n

301\uff1a\u641c\u7d22\u5f15\u64ce\u4f1a\u5c06\u6743\u91cd\u548c PR \u503c\u8fdb\u884c\u8f6c\u79fb
\n302\uff1a\u641c\u7d22\u5f15\u64ce\u4e0d\u4f1a\u8fdb\u884c\u989d\u5916\u5904\u7406
\n\u73b0\u5728\u662f\u5e0c\u671b\u641c\u7d22\u5f15\u64ce\u8ba4\u4e3a\u539f\u5730\u5740\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\uff0c\u5b8c\u5168\u8f6c\u79fb\u5230\u65b0\u5730\u5740\uff0c\u6240\u4ee5\u4f7f\u7528 301<\/p>\n

http \u8df3\u8f6c\u5230 https<\/strong><\/div>\n

\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5728 sever \u4e2d\u8fd4\u56de\u4e00\u4e2a\u91cd\u5b9a\u5411\u7684\u5730\u5740\uff0c\u4e2d\u95f4\u518d\u52a0\u4e0a 301 \u72b6\u6001\u7801\uff08\u5426\u5219\u9ed8\u8ba4\u4e3a 302\uff09<\/p>\n

\r\nserver {\r\n listen 80;\r\n return 301 https:\/\/$host$request_uri;\r\n}\r\n<\/pre>\n

return \u548c rewrite \u90fd\u5c5e\u4e8e Nginx \u7684\u91cd\u5199\u6a21\u5757\u7684\u6307\u4ee4\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4e0d\u9700\u8981\u5bf9\u8def\u5f84\u8fdb\u884c\u4fee\u6539\uff0c\u6240\u4ee5\u7528 return \u4f1a\u6bd4\u8f83\u65b9\u4fbf
\n$host \u548c$request_uri \u90fd\u662f Nginx http \u6a21\u5757\u7684\u5d4c\u5165\u53d8\u91cf\uff0c\u4e24\u4e2a\u53d8\u91cf\u5408\u5e76\u4e00\u8d77\u5c31\u76f8\u5f53\u4e8e\u628a\u8bf7\u6c42\u7684 http:\/\/ \u53bb\u6389\u7684\u7ed3\u679c<\/p>\n

www \u8df3\u8f6c\u5230\u6839\u57df\u540d<\/strong><\/div>\n

\u8fd9\u4e2a\u53ea\u9700\u5728 https \u4e2d\u4f5c\u5904\u7406\uff0c\u56e0\u4e3a\u6240\u6709 http \u90fd\u8df3\u8f6c\u5230 https \u4e86<\/p>\n

\r\nserver {\r\n listen 443 ssl;\r\n server_name ~^(?www\\.)?(.+)$;\r\n if ( $www ) {\r\n return 301 https:\/\/$2$request_uri;\r\n }\r\n...\r\n<\/www><\/pre>\n

\u8fd9\u91cc\u5229\u7528\u4e86 server_name \u7684\u6b63\u5219\u5339\u914d\u529f\u80fd\uff0c\u5728\u5176\u503c\u524d\u589e\u52a0 \uff5e \u5373\u53ef\u542f\u7528\uff0c\u652f\u6301 PCRE \u8bed\u6cd5
\n\u4f7f\u7528\u6b63\u5219\u662f\u4e3a\u4e86\u786e\u8ba4\u662f\u5426\u6709\u524d\u7f00 www. \u4ee5\u53ca\u6355\u83b7\u6839\u57df\u540d\uff0c\u751f\u6210\u4e24\u4e2a\u53d8\u91cf\uff0c\u4e00\u4e2a\u662f\u5177\u540d\u6355\u83b7\u53d8\u91cf $www \uff0c\u53e6\u4e00\u4e2a\u662f\u6570\u503c\u6355\u83b7\u53d8\u91cf $2
\nif \u91cc\u9762\u4e0d\u652f\u6301\u4f7f\u7528\u6b21\u5e8f\u6355\u83b7\u53d8\u91cf\uff0c\u5426\u5219\u4f1a\u62a5\u9519\uff08 unknown \"1\" variable\uff09\uff0c\u6240\u4ee5\u589e\u52a0\u4e86 ? \u5c06 $1 \u7684\u503c\u8d4b\u7ed9\u4e86 $www
\n<\/www><\/p>\n

\u51cf\u5c11\u8df3\u8f6c\u6b21\u6570<\/p>\n

\u4ee5\u4e0a\u8bbe\u7f6e\u5df2\u7ecf\u6ee1\u8db3\u4e86\u5b9e\u73b0\u7ed3\u679c\uff0c\u4f46\u662f\u6709\u4e00\u70b9\u7455\u75b5\uff0c\u5c31\u662f http:\/\/www.chanvinxiao.com \u4f1a\u5148\u8df3\u8f6c\u5230 https:\/\/www.chanvinxiao.com\uff0c \u518d\u8df3\u8f6c\u5230 https:\/\/chanvinxiao.com \uff0c\u8fdb\u884c\u4e8c\u6b21\u8df3\u8f6c\u80af\u5b9a\u662f\u4e0d\u5982\u53ea\u9700\u8df3\u8f6c\u4e00\u6b21\u7684\uff0c\u6240\u4ee5\u6700\u597d\u8ba9\u5176\u76f4\u63a5\u4e00\u6b65\u5230\u4f4d\uff0c\u4fee\u6539 http \u7684\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n

\r\nserver {\r\n listen 80;\r\n server_name ~^(?:www\\.)?(.+)$;\r\n return 301 https:\/\/$1$request_uri;\r\n}\r\n<\/pre>\n

\u5728 http \u5bf9\u5e94\u7684 sever \u4e2d\uff0c\u628a server_name \u4e5f\u6539\u4e3a\u6b63\u5219\u6a21\u5f0f\uff0c\u5e76\u5c06 $host \u7528\u6355\u83b7\u7684\u6839\u57df\u540d $1 \u53d6\u4ee3
\nwww \u5728\u8fd9\u91cc\u4f1a\u76f4\u63a5\u5f03\u6389\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u6355\u83b7\uff0c\u4f7f\u7528 ?: \u6807\u793a\u5b9e\u73b0\u53ea\u5206\u7ec4\u4e0d\u6355\u83b7\uff0c\u4e8e\u662f\u540e\u9762\u7684\u6839\u57df\u540d\u5c31\u6210\u4e86 $1
\n\u8fd9\u6837\u7684\u7ed3\u679c\u662f\u4e0d\u7ba1\u539f\u6765\u662f\u5426\u5e26 www\uff0c\u90fd\u7edf\u4e00\u8df3\u8f6c\u5230\u4e0d\u5e26 www \u7684 https \u6839\u57df\u540d<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u4ee5\u4e0a\u914d\u7f6e\u4e2d\u4e0d\u9700\u6307\u5b9a\u7279\u5b9a\u57df\u540d\uff0c\u53ef\u65b9\u4fbf\u517c\u5bb9\u548c\u79fb\u690d\uff0c\u4f7f\u7528\u4e86 Nginx \u7684\u4ee5\u4e0b\u7279\u6027\uff1a<\/p>\n

\r\nserver_name \u7684\u6b63\u5219\u5339\u914d\r\nreturn \u6307\u4ee4\u63a5\u6536\u72b6\u6001\u7801\u548c\u5730\u5740\r\n$host \u548c $request_uri \u5d4c\u5165\u53d8\u91cf\r\n<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8e\u4f7f\u7528Nginx\u5b9e\u73b0301\u8df3\u8f6c\u81f3https\u7684\u6839\u57df\u540d\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86,\u66f4\u591a\u76f8\u5173Nginx 301\u8df3\u8f6c\u81f3https\u6839\u57df\u540d\u5185\u5bb9\u8bf7\u641c\u7d22\u811a\u672c\u4e4b\u5bb6\u4ee5\u524d\u7684\u6587\u7ae0\u6216\u7ee7\u7eed\u6d4f\u89c8\u4e0b\u9762\u7684\u76f8\u5173\u6587\u7ae0\u5e0c\u671b\u5927\u5bb6\u4ee5\u540e\u591a\u591a\u652f\u6301\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u57fa\u4e8e SEO \u548c\u5b89\u5168\u6027\u7684\u8003\u91cf\uff0c\u9700\u8981\u8fdb\u884c 301 \u8df3\u8f6c\uff0c\u4ee5\u4e0b\u4f7f\u7528 Nginx \u4f5c\u901a\u7528\u5904\u7406 \u9700\u8981\u5c06\u4ee5\u4e0b\u5730\u5740\u90fd\u7edf\u4e00\u8df3 […]<\/p>\n","protected":false},"author":63,"featured_media":177726,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-177725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=177725"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177725\/revisions"}],"predecessor-version":[{"id":178012,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177725\/revisions\/178012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/177726"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=177725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=177725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=177725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}