{"id":146,"date":"2020-07-07T11:34:25","date_gmt":"2020-07-07T03:34:25","guid":{"rendered":"https:\/\/blog.woniufun.com\/?p=146"},"modified":"2020-07-07T11:48:22","modified_gmt":"2020-07-07T03:48:22","slug":"how-to-fix-error-unable-to-verify-the-first-certificate-in-nodejs","status":"publish","type":"post","link":"https:\/\/blog.woniufun.com\/?p=146","title":{"rendered":"How to fix &#8220;Error: unable to verify the first certificate&#8221; in nodejs"},"content":{"rendered":"<h1>\u89e3\u51b3\u95ee\u9898\u7684\u7ecf\u8fc7<\/h1>\n<p>\u4eca\u5929\u5728\u68c0\u67e5node\u670d\u52a1\u65e5\u5fd7\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<blockquote><p>\n  \u8bf7\u6c42\u4e00\u4e9b\u7f51\u7edc\u8d44\u6e90\u7684\u65f6\u5019\uff08\u4f7f\u7528\u7684requset\uff09\uff0c\u62a5\u4e86\u9519\u8bef\u201cError: unable to verify the first certificate\u201d\u3002\n<\/p><\/blockquote>\n<p>\u53ef\u4ee5\u770b\u51fa\u662f\u548c\u8bc1\u4e66\u76f8\u5173\u7684\u95ee\u9898\u3002\u56e0\u4e3a\u6211\u7684\u7f51\u7ad9\u662f\u7528\u7684\u201cLet&#8217;s Encrypt\u201d\u751f\u6210\u7684\u8bc1\u4e66\uff0c\u201cLet&#8217;s Encrypt\u201d\u867d\u7136\u5df2\u7ecf\u88ab\u5404\u4e2a\u6d4f\u89c8\u5668\u4fe1\u4efb\uff0c\u4f46\u662f\u5728\u7cfb\u7edf\u7684\u6839\u8bc1\u4e66\u91cc\u5e76\u6ca1\u6709\u5305\u542b\u5b83\u3002\u6240\u4ee5\u731c\u6d4b\u5e94\u8be5\u662f\u201cLet&#8217;s Encrypt\u201d\u5bfc\u81f4\u7684\u3002<br \/>\n\u5728\u67e5\u770b\u4e86\u53e6\u4e00\u4e2a\u540c\u6837\u51fa\u73b0\u62a5\u9519\u7684\u7f51\u7ad9\u7684\u8bc1\u4e66\u4e4b\u540e\uff0c\u9a8c\u8bc1\u4e86\u6211\u8fd9\u4e2a\u60f3\u6cd5\u3002<br \/>\n\u63a5\u4e0b\u6765\u5c31\u662f\u600e\u4e48\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u641c\u7d22\u5230\u4e86 stackoverflow \u7684\u4e00\u7bc7\u6587\u7ae0\uff1a<br \/>\nhttps:\/\/stackoverflow.com\/questions\/31673587\/error-unable-to-verify-the-first-certificate-in-nodejs<\/p>\n<p>\u6700\u9ad8\u6295\u7968\u7684\u56de\u7b54\u662f\u8fd9\u4e2a<\/p>\n<blockquote><p>\n  require(&#8216;https&#8217;).globalAgent.options.ca = require(&#8216;ssl-root-cas\/latest&#8217;).create();\n<\/p><\/blockquote>\n<p>\u5c1d\u8bd5\u4e86\u6309\u7167\u8fd9\u4e2a\u5199\u4e86\u3002\u7ed3\u679c\u53d1\u73b0\u8fd8\u662f\u62a5\u540c\u6837\u7684\u9519\u3002<br \/>\n\u6253\u5f00\u201cnode_modules\u201d\u4e0b\u7684\u201cssl-root-cas\u201d\u76ee\u5f55\uff0c\u53d1\u73b0\u91cc\u9762\u6709\u4e2apems\uff0c\u5b58\u50a8\u4e86\u8bb8\u591a\u8bc1\u4e66\u53d1\u653e\u673a\u6784\u7684\u6839\u8bc1\u4e66\uff0c\u627e\u4e86\u4e00\u4e0b\u6ca1\u6709\u53d1\u73b0\u201cLet&#8217;s Encrypt\u201d\u7684\uff0c\u53ef\u4ee5\u731c\u6d4b\u8fd9\u4e2a\u6a21\u5757\u76ee\u524d\u672a\u5c06\u201cLet&#8217;s Encrypt\u201d\u5217\u5165\u53ef\u4fe1\u4efb\u7684\u673a\u6784\u91cc\u3002\u4e8e\u662f\u9700\u8981\u5c1d\u8bd5\u5176\u4ed6\u7684\u529e\u6cd5\u3002<br \/>\n\u8fd8\u662f\u4e0a\u9762\u7684\u6587\u7ae0\uff0c\u5f80\u4e0b\u7ffb\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u6295\u7968\u6570\u4e0d\u9ad8\u7684\u56de\u7b54\uff08\u5199\u8fd9\u7bc7\u6587\u7ae0\u65f6\u7684\u6295\u7968\u6570\u662f24\uff09\uff0c\u5305\u542b\u5173\u952e\u8bcd\u201cHow do I get intermediate certificate\u201d\uff0c\u63d0\u5230\uff08\u7ffb\u8bd1\uff09<\/p>\n<blockquote><p>\n  1.\u9700\u8981\u83b7\u53d6\u7f3a\u5931\u7684.pem\u683c\u5f0f\u7684\u4e2d\u95f4\u8bc1\u4e66\uff08\u672c\u4f8b\u4e5f\u5c31\u662f\u201cLet&#8217;s Encrypt\u201d\u7684\u8bc1\u4e66\uff0c\u800c\u4e0d\u662f\u201cLet&#8217;s Encrypt\u201d\u7b7e\u7f72\u7ed9\u7f51\u7ad9\u7684\u8bc1\u4e66\uff09<br \/>\n  2a.\u4f7f\u7528node\u73af\u5883\u53d8\u91cfNODE_EXTRA_CA_CERTS\u52a0\u5165\u8bc1\u4e66<br \/>\n  2b.\u6216\u8005\uff0c\u4f7f\u7528\u4e0a\u9762\u7684 \u201cssl-root-cas\u201d\u6dfb\u52a0\u5230 \u201cca\u201d\u4e2d\n<\/p><\/blockquote>\n<p>\u6211\u9009\u62e9\u7684\u662f\u7b2c\u4e8c\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h1>\u89e3\u51b3\u65b9\u6848<\/h1>\n<p><strong>\u9996\u5148\uff0c\u8981\u5148\u627e\u5230\u201cLet&#8217;s Encrypt\u201d\u7684\u6839\u8bc1\u4e66<\/strong><br \/>\n\u65b9\u6cd51\uff1a<\/p>\n<pre><code class=\"language-shell \">#blog.woniufun.com\u4e3a\u7f51\u7ad9\u7684\u57df\u540d\uff0c\u81ea\u884c\u66ff\u6362\nopenssl s_client -connect blog.woniufun.com:443 -servername blog.woniufun.com | tee logcertfile\nopenssl x509 -in logcertfile -noout -text | grep -i \"issuer\"\n#\u672c\u4f8b\u8f93\u51fa\u201c CA Issuers - URI:http:\/\/cert.int-x3.letsencrypt.org\/\u201d\n\n#\u4e0b\u8f7d\u8bc1\u4e66\u6587\u4ef6\ncurl --output letsencrypt.crt \"http:\/\/cert.int-x3.letsencrypt.org\/\"\n#\u8f6c\u6362\u4e3apem\u683c\u5f0f\nopenssl x509 -inform DER -in letsencrypt.crt -out letsencrypt.pem -text\n<\/code><\/pre>\n<p>\u65b9\u6cd52\uff08\u9002\u7528\u4e8e\u6709\u6d4f\u89c8\u5668\uff0c\u4ee5chrome\u4e3a\u4f8b\uff09\uff1a<br \/>\n1. chrome\u6253\u5f00\u968f\u4fbf\u4e00\u4e2a\u201cLet&#8217;s Encrypt\u201d\u7b7e\u7f72\u8bc1\u4e66\u7684\u7f51\u5740\uff0c\u5982\u672c\u7ad9\u3002<br \/>\n1. \u70b9\u51fb\u5730\u5740\u680f\u5de6\u8fb9\u67e5\u770b\u8bc1\u4e66\u3002<br \/>\n1. \u5207\u6362\u5230\u201c\u8bc1\u4e66\u8def\u5f84\u201d\u6807\u7b7e\u3002<br \/>\n1. \u9009\u4e2d\u201cLet&#8217;s Encrypt Authority X3\u201d\uff0c\u70b9\u51fb\u201c\u67e5\u770b\u8bc1\u4e66\u201d\u3002<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.woniufun.com\/?attachment_id=148\"><img decoding=\"async\" src=\"https:\/\/blog.woniufun.com\/wp-content\/uploads\/2020\/07\/Snipaste_2020-07-07_11-13-20-300x284.png\" alt=\"\" \/><\/a><br \/>\n1. \u5728\u65b0\u6253\u5f00\u7684\u201c\u8bc1\u4e66\u201d\u7a97\u53e3\uff0c\u5207\u6362\u5230\u201c\u8be6\u7ec6\u4fe1\u606f\u201d\u6807\u7b7e\u9875\uff0c\u70b9\u51fb\u201c\u590d\u5236\u5230\u6587\u4ef6\u201d<br \/>\n1. \u5bfc\u51fa\u8bc1\u4e66\u7684\u65f6\u5019\uff0c\u683c\u5f0f\u9009\u62e9\u201cDER\u7f16\u7801\u201d<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.woniufun.com\/?attachment_id=149\"><img decoding=\"async\" src=\"https:\/\/blog.woniufun.com\/wp-content\/uploads\/2020\/07\/Snipaste_2020-07-07_11-15-26-300x296.png\" alt=\"\" \/><\/a><br \/>\n1. \u70b9\u51fb\u4e0b\u4e00\u6b65\u5373\u53ef\u4ee5\u9009\u62e9\u4e00\u4e2a\u8bc1\u4e66\u5b58\u50a8\u8def\u5f84\uff0c\u4fdd\u5b58\u6210\u4e00\u4e2a\u540e\u7f00\u540d\u662f.cer\u7684\u8bc1\u4e66\u3002<br \/>\n1. \u5982\u679c\u4f60\u67e5\u770b\u8fd9\u4e00\u6b65\u5bfc\u51fa\u7684.cer\u6587\u4ef6\u548c\u65b9\u6cd51\u4e0b\u8f7d\u7684crt\u6587\u4ef6\u7684md5\u503c\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u4eec\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u5c31\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\u3002<br \/>\n1. \u8f6c\u6362\u6210pem\u683c\u5f0f <code>openssl x509 -inform DER -in xxxx.crt -out letsencrypt.pem -text<\/code><\/p>\n<p><strong>\u5176\u6b21\uff0c\u5c06pem\u6587\u4ef6\u653e\u5230node\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\u5230\u7684\u5730\u65b9\uff0c\u5982\u201c\/opt\/ssl\/letsencrypt.pem\u201d<\/strong><\/p>\n<p><strong>\u6700\u540e\uff0c\u5728node\u4ee3\u7801\u4e2d\u4f7f\u7528<\/strong><\/p>\n<pre><code class=\"language-javascript \">let rootCas = require('ssl-root-cas\/latest').create();\nrootCas.addFile(\"\/opt\/ssl\/letsencrypt.pem\");\nrequire('https').globalAgent.options.ca = rootCas;\n<\/code><\/pre>\n<p>\u6b64\u65f6\u518d\u53bb\u8bf7\u6c42\u7f51\u7edc\u8d44\u6e90\uff0c\u5c31\u4e0d\u4f1a\u518d\u62a5\u9519\u4e86\u3002<\/p>\n<p><strong>\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u9002\u5408\u6240\u6709\u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42\u7684\u6a21\u5757\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u5236\u4e8e\uff1ahttps,fetch,request,axios&#8230;<\/strong><\/p>\n<h1>\u4e0d\u5efa\u8bae\u7684\u89e3\u51b3\u65b9\u6848<\/h1>\n<pre><code class=\"language-javascript \">process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u914d\u7f6e\u9879\uff0c\u5c06\u5ffd\u7565\u6240\u6709TLS\u7684\u9519\u8bef\uff0c\u8fd9\u4e2a\u76f4\u63a5\u5c06https\u7684\u4f18\u52bf\u5168\u90e8\u820d\u5f03\u4e86\u3002<\/p>\n<pre><code class=\"language-javascript \">request({method: \"GET\", \n        \"rejectUnauthorized\": false, \/\/\u6dfb\u52a0\u8fd9\u4e2a\u9009\u9879\n        \"url\": url})\n<\/code><\/pre>\n<p>\u540c\u4e0a\uff0c\u5728\u8bf7\u6c42\u7684\u65f6\u5019\u5ffd\u7565\u8bc1\u4e66\u6388\u6743\u9519\u8bef\uff0c\u540c\u6837\u5c06https\u7684\u4f18\u52bf\u5168\u90e8\u820d\u5f03\u4e86\u3002<\/p>\n<h1>\u53c2\u8003\u8d44\u6599<\/h1>\n<blockquote><p>\n  https:\/\/stackoverflow.com\/questions\/31673587\/error-unable-to-verify-the-first-certificate-in-nodejs<br \/>\n  https:\/\/www.npmjs.com\/package\/ssl-root-cas\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u6b63\u786e\u7684\u89e3\u51b3node\u4e2d\u51fa\u73b0\u7684\u201cError: unable to verify the first certificate\u201d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[18,17,19],"_links":{"self":[{"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/posts\/146"}],"collection":[{"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=146"}],"version-history":[{"count":3,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.woniufun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}