﻿{"id":4050,"date":"2020-11-17T19:45:33","date_gmt":"2020-11-17T11:45:33","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=4050"},"modified":"2020-11-17T20:43:01","modified_gmt":"2020-11-17T12:43:01","slug":"nginx-http-https","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/nginx-http-https.html","title":{"rendered":"Nginx\u5982\u4f55\u5c06HTTP\u91cd\u5b9a\u5411\u5230HTTPS"},"content":{"rendered":"<p>Nginx\uff0c\u53d1\u97f3\u4e3a\u201cEngine x\u201d\uff0c\u662f\u4e00\u4e2a\u514d\u8d39\u3001\u5f00\u6e90\u3001\u57fa\u4e8eLinux\u7684\u9ad8\u6027\u80fdWeb\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u8d1f\u8d23\u7ba1\u7406\u548c\u5904\u7406\u4e92\u8054\u7f51\u4e0a\u6700\u5927\u7684\u7f51\u7ad9\u6d41\u91cf\u7684\u8d1f\u8f7d\u3002Nginx\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u91cd\u5b9a\u5411\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u914d\u7f6e\u5728\u60a8\u7684\u7cfb\u7edf\u4e0a\u91cd\u5b9a\u5411\u4e0d\u5b89\u5168\u6216\u672a\u52a0\u5bc6\u7684HTTP\u7f51\u7edc\u6d41\u91cf\u5230\u52a0\u5bc6\u548c\u5b89\u5168\u7684HTTPS\u7f51\u7edc\u670d\u52a1\u5668\u3002\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u7cfb\u7edf\u7ba1\u7406\u5458\u6216\u5f00\u53d1\u4eba\u5458\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u7ecf\u5e38\u4f7f\u7528Nginx\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u7814\u7a76\u5982\u4f55\u5728Nginx\u4e2d\u5c06Web\u6d41\u91cf\u4eceHTTP\u91cd\u5b9a\u5411\u5230\u5b89\u5168\u7684HTTPS\u3002<\/p>\n<p>HTTP\u6d88\u606f\u5934\uff0c\u4ee5\u660e\u6587\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u4f20\u9001\uff0c\u800cHTTPS\u4f7f\u7528SSL\/TLS\u5bf9\u5ba2\u6237\u673a\u548c\u670d\u52a1\u5668\u7cfb\u7edf\u4e4b\u95f4\u7684\u901a\u4fe1\u8fdb\u884c\u52a0\u5bc6\u3002\u56e0\u6b64\uff0c\u7531\u4e8e\u8bb8\u591a\u539f\u56e0\uff0cHTTPS\u5e94\u8be5\u66ff\u4ee3HTTP\uff1a<\/p>\n<ul>\n<li>\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u4e4b\u95f4\u53cc\u5411\u7684\u6240\u6709\u6570\u636e\u5747\u5df2\u52a0\u5bc6\u3002\u4f46\u662f\uff0c\u5982\u679c\u88ab\u62e6\u622a\uff0c\u4efb\u4f55\u4eba\u90fd\u65e0\u6cd5\u8bbf\u95ee\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<li>\u5f53\u60a8\u4f7f\u7528HTTPS\u65f6\uff0cGoogle Chrome\u548c\u5176\u4ed6\u6d4f\u89c8\u5668\u4f1a\u8ba4\u4e3a\u60a8\u7684\u7f51\u7ad9\u57df\u662f\u5b89\u5168\u7684\u3002<\/li>\n<li>HTTPS\u7248\u672c\u4f7f\u7528HTTP\/2\u534f\u8bae\u6539\u5584\u4e86\u60a8\u6307\u5b9a\u7684\u7f51\u7ad9\u6027\u80fd\u3002<\/li>\n<li>\u5982\u679c\u60a8\u901a\u8fc7HTTPS\u4e3a\u60a8\u7684\u7f51\u7ad9\u57df\u63d0\u4f9b\u670d\u52a1\uff0c\u5219\u8be5\u7f51\u7ad9\u5c06\u5728Google\u4e0a\u6392\u540d\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u504f\u7231\u6240\u6709\u53d7HTTPS\u4fdd\u62a4\u7684\u7f51\u7ad9\u3002<\/li>\n<li>\u5bf9\u4e8e\u6bcf\u4e2a\u7ad9\u70b9\u7248\u672c\uff0c\u6700\u597d\u5728\u5355\u72ec\u7684\u670d\u52a1\u5668\u5757\u4e2d\u5c06Nginx\u4e2d\u7684\u6d41\u91cfHTTP\u91cd\u5b9a\u5411\u5230HTTPS\u3002\u8fd8\u5efa\u8bae\u907f\u514d\u4f7f\u7528\u201c if\u201d\u65b9\u5411\u91cd\u5b9a\u5411\u6d41\u91cf\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u670d\u52a1\u5668\u5f02\u5e38\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p><strong>\u5c06\u6240\u6709\u6d41\u91cf\u4eceHTTP\u91cd\u5b9a\u5411\u5230HTTPS<\/strong><\/p>\n<p>\u5c06\u4ee5\u4e0b\u66f4\u6539\u6dfb\u52a0\u5230Nginx\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u5c06\u6240\u6709\u6d41\u91cf\u4eceHTTP\u91cd\u5b9a\u5411\u5230HTTPS\u7248\u672c\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #292824; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a6a28c; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\">server {\r\n    listen <span style=\"color: #b65611;\">80<\/span> default_server;\r\n    server_name _;\r\n    <span style=\"color: #b854d4;\">return<\/span> <span style=\"color: #b65611;\">301<\/span> https:<span style=\"color: #999580;\">\/\/$host$request_uri;<\/span>\r\n}\r\n<\/code><\/pre>\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u8be6\u7ec6\u8bf4\u660e\u6bcf\u4e2a\u4e0a\u8ff0\u672f\u8bed\uff1a<\/p>\n<p>Listen 80 default_server &#8211; \u8fd9\u5c06\u6307\u793a\u60a8\u7684\u7cfb\u7edf\u6355\u83b7\u7aef\u53e380\u4e0a\u7684\u6240\u6709HTTP\u901a\u4fe1<\/p>\n<p>Server_name _ &#8211; \u5728\u63a5\u5230\u8bf7\u6c42\u540e\u7684\u5339\u914d\u987a\u5e8f<\/p>\n<p>Return 301 https:\/\/$host$request_uri &#8211; \u8fd9\u544a\u8bc9\u60a8\u7684\u641c\u7d22\u5f15\u64ce\u5c06\u5176\u6c38\u4e45\u91cd\u5b9a\u5411\u3002\u5b83\u6307\u5b9a\u53d8\u91cf$host \u4fdd\u5b58\u57df\u540d\u3002<\/p>\n<p>\u66f4\u6539\u914d\u7f6e\u8bbe\u7f6e\u540e\uff0c\u9700\u8981\u5728\u7cfb\u7edf\u4e0a\u91cd\u65b0\u52a0\u8f7dNginx\u670d\u52a1\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u52a0\u8f7dNginx\u670d\u52a1\uff1a<\/p>\n<p>$ sudo systemctl reload nginx<\/p>\n<p><strong>\u5728Nginx\u4e2d\u5c06\u6307\u5b9a\u57df\u540d\u7684HTTP\u91cd\u5b9a\u5411\u5230HTTPS<\/strong><\/p>\n<p>\u5728\u60a8\u7684\u57df\u4e0a\u5b89\u88c5SSL\u8bc1\u4e66\u4e4b\u540e\uff0c\u60a8\u5c06\u6709\u4e24\u4e2a\u7528\u4e8e\u6b64\u57df\u540d\u7684\u670d\u52a1\u5668\u5757\u9009\u9879\u3002\u4e00\u4e2a\u5757\u7528\u4e8e\u76d1\u542c\u7aef\u53e380\u7684HTTP\u7248\u672c\uff0c\u7b2c\u4e8c\u4e2a\u7248\u672c\u7528\u4e8e\u76d1\u542c\u7aef\u53e3443\u7684HTTPS\u3002\u7136\u800c\uff0c\u8981\u5c06\u4e00\u4e2a\u7f51\u7ad9\u57df\u540d\u4eceHTTP\u91cd\u5b9a\u5411\u5230HTTPS\uff0c\u4f60\u9700\u8981\u6253\u5f00Nginx\u914d\u7f6e\u3002\u60a8\u53ef\u4ee5\u5728\/etc\/nginx\/sites-available\u76ee\u5f55\u4e2d\u627e\u5230\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002\u5982\u679c\u4f60\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u641c\u7d22\u5b83\/etc\/nginx\/nginx.\/usr\/local\/nginx\/conf\u6216\/usr\/local\/etc\/nginx\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u6267\u884c\u4ee5\u4e0b\u66f4\u6539:<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #292824; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a6a28c; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\">server {\r\n    listen <span style=\"color: #b65611;\">80<\/span>;\r\n    server_name linuxmi.com <a href=\"http:\/\/www.linuxmi.com\/\">www.linuxmi.com<\/a>;\r\n    <span style=\"color: #b854d4;\">return<\/span> <span style=\"color: #b65611;\">301<\/span> <a href=\"https:\/\/www.linuxmi.com$request_uri\/\">https:<\/a><span style=\"color: #999580;\"><a>\/\/linuxmi.com$request_uri<\/a>;<\/span>\r\n}\r\n<\/code><\/pre>\n<p>\u8ba9\u6211\u4eec\u9010\u884c\u4e86\u89e3\u4e0a\u9762\u7684\u4ee3\u7801\u3002<\/p>\n<p>Listen 80 &#8211; \u4f7f\u7528\u7aef\u53e380\uff0c\u670d\u52a1\u5668\u5c06\u4fa6\u542c\u6307\u5b9a\u57df\u540d\u7684\u6240\u6709\u4f20\u5165\u8fde\u63a5\u3002<\/p>\n<p>Server_name linuxmi.com www.linuxmi.com &#8211; \u5b83\u6307\u5b9a\u57df\u540d\u3002\u56e0\u6b64\uff0c\u5c06\u5176\u66ff\u6362\u4e3a\u60a8\u8981\u91cd\u5b9a\u5411\u7684\u7f51\u7ad9\u57df\u540d\u3002<\/p>\n<p>Return 301 https:\/\/linuxmi.com$request_uri &#8211; \u5c06\u6d41\u91cf\u79fb\u81f3\u8be5\u7ad9\u70b9\u7684HTTPS\u7248\u672c\u3002$request_uri\u53d8\u91cf\u7528\u4e8e\u5b8c\u6574\u7684\u539f\u59cb\u8bf7\u6c42URI\uff0c\u5176\u4e2d\u4e5f\u5305\u542b\u53c2\u6570\u3002<\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5c06\u6d41\u91cf\u91cd\u5b9a\u5411\u5230HTTPS www\u7248\u672c\u5230\u7ad9\u70b9\u7684\u975ewww\u7248\u672c\u3002\u5bf9\u4e8e\u975ewww\u548cwww\u7248\u672c\uff0c\u5efa\u8bae\u5728\u5355\u72ec\u7684\u670d\u52a1\u5668\u5757\u4e2d\u521b\u5efa\u91cd\u5b9a\u5411\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u4f8b\u5b50\u6765\u89e3\u91ca\u3002\u5982\u679c\u8981\u5c06www HTTPS\u8bf7\u6c42\u91cd\u5b9a\u5411\u5230\u975ewww\u7248\u672c\uff0c\u5219\u5e94\u9075\u5faa\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #292824; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a6a28c; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\">server {\r\n    listen <span style=\"color: #b65611;\">80<\/span>;\r\n    server_name linuxmi.com www.linuxmi.com;\r\n    <span style=\"color: #b854d4;\">return<\/span> <span style=\"color: #b65611;\">301<\/span> https:<span style=\"color: #999580;\">\/\/linuxmi.com$request_uri;<\/span>\r\n}\r\nserver {\r\n    listen <span style=\"color: #b65611;\">443<\/span> ssl http2;\r\n    server_name www.linuxmi.com;\r\n    # . . . other code\r\n    <span style=\"color: #b854d4;\">return<\/span> <span style=\"color: #b65611;\">301<\/span> https:<span style=\"color: #999580;\">\/\/linuxmi.com$request_uri;<\/span>\r\n}\r\nserver {\r\n    listen <span style=\"color: #b65611;\">443<\/span> ssl http2;\r\n    server_name linuxmi.com;\r\n\r\n    # . . . other code\r\n}\r\n<\/code><\/pre>\n<p>\u7528\u60a8\u7684\u57df\u540d\u66ff\u6362\u57df\u540d\uff0c\u4f8b\u5982www.linuxmi.com\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u4e86\u5982\u4f55\u5c06\u6d41\u91cf\u4eceHTTP\u7248\u672c\u91cd\u5b9a\u5411\u5230Nginx\u670d\u52a1\u5668\u4e0a\u7684HTTPS\u3002\u901a\u8fc7\u6539\u53d8Nginx\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6307\u5b9a\u57df\u540d\u6216\u5168\u90e8\u91cd\u5b9a\u5411\u5230HTTPS\u3002\u6211\u4eec\u5728\u672c\u6587\u4e2d\u63d0\u5230\u7684\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u7528\u6237\u4f53\u9a8c\uff0c\u4f7f\u60a8\u7684\u7f51\u7ad9\u66f4\u5b89\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx\uff0c\u53d1\u97f3\u4e3a\u201cEngine x\u201d\uff0c\u662f\u4e00\u4e2a\u514d\u8d39\u3001\u5f00\u6e90\u3001\u57fa\u4e8eLinux\u7684\u9ad8\u6027\u80fdWeb\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[373],"class_list":["post-4050","post","type-post","status-publish","format-standard","hentry","category-linux","tag-nginx"],"aioseo_notices":[],"views":724,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=4050"}],"version-history":[{"count":3,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4050\/revisions"}],"predecessor-version":[{"id":4053,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/4050\/revisions\/4053"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=4050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=4050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=4050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}