{"id":51169,"date":"2024-10-08T16:35:15","date_gmt":"2024-10-08T08:35:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51169\/"},"modified":"2024-10-08T16:35:15","modified_gmt":"2024-10-08T08:35:15","slug":"nginx-%e9%85%8d%e7%bd%ae-ssl-%e8%ad%89%e6%9b%b8-%e6%90%ad%e5%bb%ba-https-%e7%b6%b2%e7%ab%99%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51169\/","title":{"rendered":"Nginx \u914d\u7f6e SSL \u8b49\u66f8 + \u642d\u5efa HTTPS \u7db2\u7ad9\u6559\u7a0b"},"content":{"rendered":"<h1 id=\"nginx-%e9%85%8d%e7%bd%ae-ssl-%e8%ad%89%e6%9b%b8-%e6%90%ad%e5%bb%ba-https-%e7%b6%b2%e7%ab%99%e6%95%99%e7%a8%8b-vnmKovNqzI\">Nginx \u914d\u7f6e SSL \u8b49\u66f8 + \u642d\u5efa HTTPS \u7db2\u7ad9\u6559\u7a0b<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u4f7f\u7528 HTTPS \u5354\u8b70\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u7528\u6236\u7684\u6578\u64da\u5b89\u5168\uff0c\u4e26\u63d0\u9ad8\u7db2\u7ad9\u7684\u4fe1\u4efb\u5ea6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Nginx \u4e0a\u914d\u7f6e SSL \u8b49\u66f8\uff0c\u4e26\u642d\u5efa\u4e00\u500b\u5b89\u5168\u7684 HTTPS \u7db2\u7ad9\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ssl-%e8%ad%89%e6%9b%b8%ef%bc%9f-vnmKovNqzI\">\u4ec0\u9ebc\u662f SSL \u8b49\u66f8\uff1f<\/h2>\n<p>SSL\uff08\u5b89\u5168\u5957\u63a5\u5b57\u5c64\uff09\u8b49\u66f8\u662f\u4e00\u7a2e\u6578\u5b57\u8b49\u66f8\uff0c\u7528\u65bc\u52a0\u5bc6\u7528\u6236\u8207\u7db2\u7ad9\u4e4b\u9593\u7684\u6578\u64da\u50b3\u8f38\u3002\u5b83\u80fd\u5920\u78ba\u4fdd\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u4e0d\u88ab\u7aca\u53d6\u6216\u7be1\u6539\u3002\u7576\u7db2\u7ad9\u4f7f\u7528 SSL \u8b49\u66f8\u6642\uff0cURL \u6703\u986f\u793a\u70ba <code>https:\/\/<\/code>\uff0c\u800c\u4e0d\u662f <code>http:\/\/<\/code>\u3002<\/p>\n<h2 id=\"%e6%ba%96%e5%82%99%e5%b7%a5%e4%bd%9c-vnmKovNqzI\">\u6e96\u5099\u5de5\u4f5c<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5b8c\u6210\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u64c1\u6709\u4e00\u500b\u57df\u540d\u3002<\/li>\n<li>\u64c1\u6709\u4e00\u53f0\u904b\u884c Nginx \u7684\u4f3a\u670d\u5668\u3002<\/li>\n<li>\u7372\u53d6 SSL \u8b49\u66f8\u3002\u60a8\u53ef\u4ee5\u9078\u64c7\u8cfc\u8cb7\u5546\u696d\u8b49\u66f8\u6216\u4f7f\u7528\u514d\u8cbb\u7684 Let&#8217;s Encrypt \u8b49\u66f8\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-vnmKovNqzI\">\u5b89\u88dd Nginx<\/h2>\n<p>\u5982\u679c\u60a8\u7684\u4f3a\u670d\u5668\u5c1a\u672a\u5b89\u88dd Nginx\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx<\/code><\/pre>\n<h2 id=\"%e7%8d%b2%e5%8f%96-ssl-%e8%ad%89%e6%9b%b8-vnmKovNqzI\">\u7372\u53d6 SSL \u8b49\u66f8<\/h2>\n<p>\u9019\u88e1\u4ee5 Let&#8217;s Encrypt \u70ba\u4f8b\uff0c\u4f7f\u7528 Certbot \u7372\u53d6\u514d\u8cbb\u7684 SSL \u8b49\u66f8\u3002\u9996\u5148\uff0c\u5b89\u88dd Certbot\uff1a<\/p>\n<pre><code>sudo apt install certbot python3-certbot-nginx<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7372\u53d6\u8b49\u66f8\uff1a<\/p>\n<pre><code>sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com<\/code><\/pre>\n<p>\u6839\u64da\u63d0\u793a\u5b8c\u6210\u8b49\u66f8\u7684\u7372\u53d6\u904e\u7a0b\u3002Certbot \u6703\u81ea\u52d5\u70ba Nginx \u914d\u7f6e SSL\u3002<\/p>\n<h2 id=\"%e6%89%8b%e5%8b%95%e9%85%8d%e7%bd%ae-nginx-vnmKovNqzI\">\u624b\u52d5\u914d\u7f6e Nginx<\/h2>\n<p>\u5982\u679c\u60a8\u9700\u8981\u624b\u52d5\u914d\u7f6e Nginx\uff0c\u8acb\u6253\u958b Nginx \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n<p>\u5728 server \u5340\u584a\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name yourdomain.com www.yourdomain.com;\n    return 301 https:\/\/$host$request_uri;\n}\n\nserver {\n    listen 443 ssl;\n    server_name yourdomain.com www.yourdomain.com;\n\n    ssl_certificate \/etc\/letsencrypt\/live\/yourdomain.com\/fullchain.pem;\n    ssl_certificate_key \/etc\/letsencrypt\/live\/yourdomain.com\/privkey.pem;\n\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n}<\/code><\/pre>\n<p>\u78ba\u4fdd\u5c07 <code>yourdomain.com<\/code> \u66ff\u63db\u70ba\u60a8\u7684\u5be6\u969b\u57df\u540d\u3002<\/p>\n<h2 id=\"%e6%b8%ac%e8%a9%a6-nginx-%e9%85%8d%e7%bd%ae-vnmKovNqzI\">\u6e2c\u8a66 Nginx \u914d\u7f6e<\/h2>\n<p>\u5728\u91cd\u65b0\u555f\u52d5 Nginx \u4e4b\u524d\uff0c\u5efa\u8b70\u5148\u6e2c\u8a66\u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u6709\u932f\u8aa4\uff0c\u5247\u53ef\u4ee5\u91cd\u65b0\u555f\u52d5 Nginx\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<h2 id=\"%e8%87%aa%e5%8b%95%e7%ba%8c%e6%9c%9f-ssl-%e8%ad%89%e6%9b%b8-vnmKovNqzI\">\u81ea\u52d5\u7e8c\u671f SSL \u8b49\u66f8<\/h2>\n<p>Let&#8217;s Encrypt \u7684\u8b49\u66f8\u6709\u6548\u671f\u70ba 90 \u5929\uff0c\u56e0\u6b64\u9700\u8981\u5b9a\u671f\u7e8c\u671f\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u81ea\u52d5\u7e8c\u671f\uff1a<\/p>\n<pre><code>sudo certbot renew --dry-run<\/code><\/pre>\n<p>\u9019\u5c07\u6a21\u64ec\u7e8c\u671f\u904e\u7a0b\uff0c\u78ba\u4fdd\u4e00\u5207\u6b63\u5e38\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-vnmKovNqzI\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6210\u529f\u5728 Nginx \u4e0a\u914d\u7f6e SSL \u8b49\u66f8\uff0c\u4e26\u642d\u5efa\u4e00\u500b\u5b89\u5168\u7684 HTTPS \u7db2\u7ad9\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u9084\u80fd\u589e\u5f37\u7528\u6236\u7684\u4fe1\u4efb\u611f\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u7684\u6280\u8853\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Nginx \u4e2d\u914d\u7f6e SSL \u8b49\u66f8\uff0c\u642d\u5efa\u5b89\u5168\u7684 HTTPS \u7db2\u7ad9\uff0c\u4fdd\u8b77\u7528\u6236\u6578\u64da\uff0c\u63d0\u5347\u7db2\u7ad9\u4fe1\u4efb\u5ea6\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-51169","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51169","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=51169"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51169\/revisions"}],"predecessor-version":[{"id":51170,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51169\/revisions\/51170"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}