{"id":207981,"date":"2025-07-08T10:52:17","date_gmt":"2025-07-08T02:52:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207981\/"},"modified":"2025-07-08T10:52:17","modified_gmt":"2025-07-08T02:52:17","slug":"caddy-server-certmagic-%e6%97%a0%e6%95%88%e5%86%85%e5%ad%98%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207981\/","title":{"rendered":"Caddy Server Certmagic &#8211; \u65e0\u6548\u5185\u5b58\u5730\u5740"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Caddy Server Certmagic &#8211; \u65e0\u6548\u5185\u5b58\u5730\u5740<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 15:36:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aCaddy Server Certmagic &#8211; \u65e0\u6548\u5185\u5b58\u5730\u5740\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 certmagic \u8bbe\u7f6e\u670d\u52a1\u5668\uff0c\u4f46\u6bcf\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6\uff0c\u6211\u90fd\u4f1a\u6536\u5230\u4ee5\u4e0b\u5806\u6808\u6d88\u606f\uff0c\u6211\u5728\u7f51\u4e0a\u67e5\u770b\u7684\u6240\u6709\u5730\u65b9\u90fd\u8bf4\u60a8\u53ea\u9700\u8981\u6267\u884c <code>certmagic.https([]string{\"my- domain.com\"}\uff0c\u8def\u7531\u5668\uff09<\/code> \u4f46\u8fd9\u91cc\u4e00\u5b9a\u7f3a\u5c11\u4e00\u4e9b\u4e1c\u897f\uff1f<\/p>\n<pre>panic: runtime error: invalid memory address or nil pointer dereference\n[signal sigsegv: segmentation violation code=0x1 addr=0x0 pc=0x6dd407]\n\ngoroutine 1 [running]:\ngo.uber.org\/zap.(*logger).check(0x0, 0xff, 0xc000026858, 0x11, 0xc000064f40)\n    \/...\/go\/pkg\/mod\/go.uber.org\/[email&nbsp;protected]\/logger.go:265 +0x987\ngo.uber.org\/zap.(*logger).debug(0x0, 0xc000026858, 0x11, 0xc000064f40, 0x1, 0x1)\n    \/...\/go\/pkg\/mod\/go.uber.org\/[email&nbsp;protected]\/logger.go:180 +0x45\ngithub.com\/caddyserver\/certmagic.(*config).obtaincert.func2(0x8f2e70, 0xc0000240e0, 0x8f5370, 0xb14d30)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:511 +0x5c5\ngithub.com\/caddyserver\/certmagic.(*config).obtaincert(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0x873baf, 0x16, 0x1, 0x0, 0x0)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:566 +0x3a8\ngithub.com\/caddyserver\/certmagic.(*config).obtaincertsync(...)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:421\ngithub.com\/caddyserver\/certmagic.(*config).manageone.func1(0x842da0, 0xc000064400)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:334 +0x412\ngithub.com\/caddyserver\/certmagic.(*config).manageone(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0x873baf, 0x16, 0x0, 0x0, 0x8f5370)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:361 +0x27b\ngithub.com\/caddyserver\/certmagic.(*config).manageall(0xc0000952c0, 0x8f2e70, 0xc0000240e0, 0xc000020f60, 0x1, 0x1, 0xb45100, 0x0, 0x0)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:312 +0x1f1\ngithub.com\/caddyserver\/certmagic.(*config).managesync(...)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/config.go:251\ngithub.com\/caddyserver\/certmagic.https(0xc000020f60, 0x1, 0x1, 0x8ec800, 0xc0000da0c0, 0x0, 0x0)\n    \/...\/go\/pkg\/mod\/github.com\/caddyserver\/[email&nbsp;protected]\/certmagic.go:76 +0xdb\nmain.main()\n    \/var\/www\/test\/main.go:17 +0xe7<\/pre>\n<p>\u76ee\u524d\u7684\u4ee3\u7801\u662f\u6700\u4f4e\u9650\u5ea6\u7684\uff0c\u56e0\u4e3a\u5728\u8003\u8651\u5c06\u5176\u6269\u5c55\u5230\u5176\u4ed6\u9879\u76ee\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u662f\u4ece\u5934\u5f00\u59cb\u4e86\u89e3\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684<\/p>\n<pre>package main\n\nimport (\n    \"github.com\/caddyserver\/certmagic\"\n    \"github.com\/gorilla\/mux\"\n    \"log\"\n    \"net\/http\"\n)\n\nfunc main() {\n    router := mux.newrouter()\n    router.handlefunc(\"\/\", func (w http.responsewriter, r *http.request) {\n        _, _ = w.write([]byte(\"hello world\"))\n    })\n    log.fatal(certmagic.https([]string{\"my-domain.com\"}, router))\n}<\/pre>\n<p>\u751a\u81f3\u66f4\u7b80\u5355\uff08\u4ee5\u53ca certmagic \u4e0a\u63d0\u4f9b\u7684\u793a\u4f8b\uff09\u4e5f\u4f1a\u5f97\u5230\u76f8\u540c\u7684\u9519\u8bef\u5806\u6808<\/p>\n<pre>func main() {\n    handler := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {\n        _, _ = w.Write([]byte(\"Hello world\"))\n    })\n    certmagic.HTTPS([]string{\"my-domain.com\"}, handler)\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f certmagic \u4e2d\u7684\u4e00\u4e2a\u5df2\u77e5\u9519\u8bef\uff0c\u5df2\u4fee\u590d\u5e76\u5408\u5e76\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728\u6b64\u5904\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\uff1a<\/p>\n<p>\u5b83\u4eec\u4f3c\u4e4e\u7ecf\u5e38\u53d1\u5e03\uff0c\u56e0\u6b64\u53ef\u4ee5\u7b49\u5f85\u51e0\u5929\u53d1\u5e03\u65b0\u7248\u672c\uff0c\u6216\u8005\u540c\u65f6\u4f7f\u7528\u4e3b\u5206\u652f\u800c\u4e0d\u662f v0.14.0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aCaddy Server Certmagic &#8211; \u65e0\u6548\u5185\u5b58\u5730\u5740\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207981","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207981","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=207981"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207981\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}