{"id":50963,"date":"2024-10-08T15:01:58","date_gmt":"2024-10-08T07:01:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/50963\/"},"modified":"2024-10-08T15:01:59","modified_gmt":"2024-10-08T07:01:59","slug":"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e9%85%8d%e7%bd%ae-ssl-%e5%8d%94%e8%ad%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/50963\/","title":{"rendered":"Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u914d\u7f6e SSL \u5354\u8b70"},"content":{"rendered":"<h1 id=\"nginx-%e6%96%b0%e6%89%8b%e6%95%99%e7%a8%8b%ef%bc%9a%e9%85%8d%e7%bd%ae-ssl-%e5%8d%94%e8%ad%b0-LYpMLIlhWu\">Nginx \u65b0\u624b\u6559\u7a0b\uff1a\u914d\u7f6e SSL \u5354\u8b70<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u7db2\u7ad9\u904b\u71df\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002SSL\uff08\u5b89\u5168\u5957\u63a5\u5c64\uff09\u5354\u8b70\u80fd\u5920\u52a0\u5bc6\u7528\u6236\u8207\u7db2\u7ad9\u4e4b\u9593\u7684\u6578\u64da\u50b3\u8f38\uff0c\u4fdd\u8b77\u7528\u6236\u7684\u96b1\u79c1\u548c\u6578\u64da\u5b89\u5168\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Nginx \u4e0a\u914d\u7f6e SSL \u5354\u8b70\uff0c\u5e6b\u52a9\u65b0\u624b\u7528\u6236\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ssl-%e5%8d%94%e8%ad%b0%ef%bc%9f-LYpMLIlhWu\">\u4ec0\u9ebc\u662f SSL \u5354\u8b70\uff1f<\/h2>\n<p>SSL\uff08Secure Sockets Layer\uff09\u662f\u4e00\u7a2e\u5b89\u5168\u5354\u8b70\uff0c\u65e8\u5728\u4fdd\u8b77\u4e92\u806f\u7db2\u901a\u4fe1\u7684\u5b89\u5168\u6027\u3002\u5b83\u901a\u904e\u52a0\u5bc6\u6578\u64da\u4f86\u9632\u6b62\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u88ab\u7aca\u53d6\u6216\u7be1\u6539\u3002\u7576\u7db2\u7ad9\u4f7f\u7528 SSL \u5354\u8b70\u6642\uff0c\u7db2\u5740\u6703\u986f\u793a\u70ba <code>https:\/\/<\/code>\uff0c\u800c\u975e <code>http:\/\/<\/code>\uff0c\u9019\u8868\u793a\u8a72\u7db2\u7ad9\u662f\u5b89\u5168\u7684\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-nginx%ef%bc%9f-LYpMLIlhWu\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Nginx\uff1f<\/h2>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684 HTTP \u548c\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u56e0\u5176\u8f15\u91cf\u7d1a\u548c\u9ad8\u6548\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u5b83\u4e0d\u50c5\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u9023\u63a5\uff0c\u9084\u80fd\u5920\u8f15\u9b06\u914d\u7f6e SSL \u5354\u8b70\uff0c\u9069\u5408\u5404\u985e\u578b\u7684\u7db2\u7ad9\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae-ssl-%e7%9a%84%e6%ad%a5%e9%a9%9f-LYpMLIlhWu\">\u914d\u7f6e SSL \u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e7%8d%b2%e5%8f%96-ssl-%e8%ad%89%e6%9b%b8-LYpMLIlhWu\">1. \u7372\u53d6 SSL \u8b49\u66f8<\/h3>\n<p>\u5728\u914d\u7f6e SSL \u4e4b\u524d\uff0c\u60a8\u9700\u8981\u7372\u53d6\u4e00\u500b SSL \u8b49\u66f8\u3002\u60a8\u53ef\u4ee5\u9078\u64c7\u8cfc\u8cb7\u5546\u696d\u8b49\u66f8\uff0c\u6216\u4f7f\u7528\u514d\u8cbb\u7684 Let&#8217;s Encrypt \u8b49\u66f8\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Let&#8217;s Encrypt \u7372\u53d6\u8b49\u66f8\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>sudo apt-get install certbot python3-certbot-nginx\nsudo certbot --nginx -d yourdomain.com -d www.yourdomain.com<\/code><\/pre>\n<p>\u9019\u689d\u547d\u4ee4\u6703\u81ea\u52d5\u70ba\u60a8\u7684\u57df\u540d\u751f\u6210 SSL \u8b49\u66f8\u4e26\u914d\u7f6e Nginx\u3002<\/p>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-nginx-LYpMLIlhWu\">2. \u914d\u7f6e Nginx<\/h3>\n<p>\u7372\u53d6\u8b49\u66f8\u5f8c\uff0c\u60a8\u9700\u8981\u914d\u7f6e Nginx \u4ee5\u4f7f\u7528 SSL\u3002\u6253\u958b Nginx \u914d\u7f6e\u6587\u4ef6\uff0c\u901a\u5e38\u4f4d\u65bc <code>\/etc\/nginx\/sites-available\/default<\/code> \u6216 <code>\/etc\/nginx\/nginx.conf<\/code>\u3002<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/default<\/code><\/pre>\n<p>\u5728 server \u5340\u584a\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\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        proxy_pass http:\/\/localhost:3000;  # \u6839\u64da\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u8abf\u6574\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n        proxy_cache_bypass $http_upgrade;\n    }\n}<\/code><\/pre>\n<h3 id=\"3-%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae-LYpMLIlhWu\">3. \u6e2c\u8a66\u914d\u7f6e<\/h3>\n<p>\u5728\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u5f8c\uff0c\u60a8\u9700\u8981\u6e2c\u8a66 Nginx \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\u60a8\u53ef\u4ee5\u91cd\u65b0\u555f\u52d5 Nginx \u4ee5\u61c9\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<h3 id=\"4-%e7%a2%ba%e8%aa%8d-ssl-%e9%85%8d%e7%bd%ae-LYpMLIlhWu\">4. \u78ba\u8a8d SSL \u914d\u7f6e<\/h3>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u700f\u89bd\u5668\u8a2a\u554f\u60a8\u7684\u7db2\u7ad9\uff0c\u78ba\u8a8d\u662f\u5426\u6210\u529f\u555f\u7528 SSL\u3002\u60a8\u61c9\u8a72\u80fd\u5920\u770b\u5230\u7da0\u8272\u7684\u9396\u5f62\u5716\u6a19\uff0c\u8868\u793a\u7db2\u7ad9\u662f\u5b89\u5168\u7684\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LYpMLIlhWu\">\u7e3d\u7d50<\/h2>\n<p>\u914d\u7f6e SSL \u5354\u8b70\u662f\u63d0\u5347\u7db2\u7ad9\u5b89\u5168\u6027\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u5728 Nginx \u4e0a\u6210\u529f\u914d\u7f6e SSL\uff0c\u4fdd\u8b77\u7528\u6236\u7684\u6578\u64da\u5b89\u5168\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 \u5354\u8b70\uff0c\u4fdd\u8b77\u7db2\u7ad9\u5b89\u5168\uff0c\u63d0\u5347\u7528\u6236\u4fe1\u4efb\uff0c\u7c21\u55ae\u6613\u61c2\u7684\u65b0\u624b\u6559\u7a0b\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-50963","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50963","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=50963"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50963\/revisions"}],"predecessor-version":[{"id":50964,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/50963\/revisions\/50964"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=50963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=50963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=50963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}