{"id":4455,"date":"2022-09-18T19:44:27","date_gmt":"2022-09-18T11:44:27","guid":{"rendered":"https:\/\/www.chenqing.work\/?p=4455"},"modified":"2022-09-18T20:11:30","modified_gmt":"2022-09-18T12:11:30","slug":"%e7%ac%ac%e4%b8%80%e6%bb%b4%e8%a1%80%e4%b9%8b-caddy","status":"publish","type":"post","link":"https:\/\/www.chenqing.work\/?p=4455","title":{"rendered":"\u65b0\u8f6e\u5b50 Caddy \u5165\u624b\u4f53\u9a8c"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1440\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/pete-haas-4me4b_2dRCY-unsplash.jpg\" alt=\"\" class=\"wp-image-4506\"\/><\/figure>\n\n\n\n<p>Go\u3001Rust \u8fd9\u4fe9\u8bed\u8a00\u706b\u8d77\u6765\u540e\uff0c\u53c8\u7206\u53d1\u4e86\u4e00\u6ce2\u9020\u8f6e\u5b50\u7684\u70ed\u6f6e\u3002Go \u6709\u5927\u540d\u9f0e\u9f0e\u7684 Kubernetes\uff0cRust \u4e5f\u5373\u5c06\u51fa\u73b0\u5728 linux \u5185\u6838\u7684 6.1 \u7248\u672c\u4e2d\uff0c\u9664\u4e86\u8fd9\u4e9b\u6740\u624b\u7ea7\u4f7f\u7528\u573a\u666f\uff0c\u8fd8\u6709\u4e00\u4e9b\u8f83\u5c0f\u7684\u8f6e\u5b50\uff0c\u57fa\u672c\u90fd\u662f\u6740\u624b\u7ea7\u573a\u666f\u7684\u884d\u751f\u573a\u666f\uff0cGo \u4e13\u6ce8\u4e91\u573a\u666f\u3001\u540e\u7aef\u573a\u666f\uff0cRust \u4e13\u6ce8 linux \u73af\u5883\u2014\u2014\u6700\u8fd1\u597d\u50cf\u4e0d\u5c11\u4eba\u5728\u7528 Rust \u72c2\u5199\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4eca\u5929\u8981\u4f53\u9a8c\u7684 <a href=\"https:\/\/caddyserver.com\/\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/\">Caddy <\/a>\u5c31\u662f\u7528 Go \u5199\u7684 Web \u670d\u52a1\u5668\uff0cGithub 43k star \u7684\u9879\u76ee\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u7528\u9014\uff1a<a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/file_server\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/file_server\">Web \u670d\u52a1\u5668<\/a>\u3001<a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy\">\u53cd\u5411\u4ee3\u7406<\/a>\u3002\u770b\u8d77\u6765\u548c Nginx \u5c5e\u4e8e\u540c\u7c7b\u4ea7\u54c1\u3002\u5148\u770b\u770b\u4e2b\u662f\u600e\u4e48\u5ba2\u89c2\u5730\u5439\u81ea\u5df1\u7684\ud83e\udd14\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"905\" height=\"688\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-7.png\" alt=\"\" class=\"wp-image-4553\"\/><\/figure>\n\n\n\n<p>\u6d4f\u89c8\u4e00\u904d\u6211\u4eec\u76f4\u63a5\u4e0a\u8def\uff0c\u5728 Fedora 36 \u4e0a\u7684\u5b89\u88c5\u4f53\u9a8c\u6bd4\u8f83\u4e1d\u6ed1\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncq@fedora ~&gt; sudo dnf install 'dnf-command(copr)'\ncq@fedora ~&gt; sudo dnf copr enable @caddy\/caddy\ncq@fedora ~&gt; sudo dnf install caddy\n<\/pre><\/div>\n\n\n<p>\u4e09\u6761\u547d\u4ee4\u6267\u884c\u8fc7\u540e\uff0c\u5c31\u7b97\u5b89\u88c5\u6210\u529f\u4e86\uff0c\u53ef\u4ee5\u5728 \/lib\/systemd\/system\/ \u4e0b\u627e\u5230 caddy.service\u3001caddy-api.service \u4e24\u4e2a\u670d\u52a1\u3002\u547d\u4ee4\u884c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6267\u884c caddy\uff0c\u6307\u5411 \/usr\/bin\/caddy\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"609\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image.png\" alt=\"\" class=\"wp-image-4527\"\/><\/figure>\n\n\n\n<p>\u4f7f\u7528 caddy run \u5c31\u53ef\u4ee5\u6309\u9ed8\u8ba4\u914d\u7f6e\u628a\u5e94\u7528\u8d77\u8d77\u6765\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"917\" height=\"83\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-1.png\" alt=\"\" class=\"wp-image-4530\"\/><\/figure>\n\n\n\n<p>Caddy \u4e3b\u8981\u652f\u6301\u4e09\u79cd\u914d\u7f6e\u65b9\u5f0f\uff1a<strong><a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\">Caddyfile<\/a><\/strong>\u3001<strong>caddy.json<\/strong>\u3001<strong><a href=\"https:\/\/caddyserver.com\/docs\/api-tutorial\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/api-tutorial\">API<\/a><\/strong>\u3002\u524d\u4e24\u79cd\u90fd\u662f\u9759\u6001\u914d\u7f6e\u6587\u4ef6\uff0c\u6700\u540e\u4e00\u79cd\u662f\u52a8\u6001\u914d\u7f6e\u3002\u9664\u4e86\u4e3b\u6d41\u7684\u4e09\u79cd\uff0c\u4e5f\u6709\u82e5\u5e72 config-adapter \u652f\u6301\u4ece\u5176\u4ed6\u914d\u7f6e\u683c\u5f0f\u8f6c\u8bd1\u8fc7\u6765\uff0c\u5982 <a href=\"https:\/\/github.com\/caddyserver\/nginx-adapter\" data-type=\"URL\" data-id=\"https:\/\/github.com\/caddyserver\/nginx-adapter\">nginx<\/a>\u3001<a href=\"https:\/\/github.com\/abiosoft\/caddy-yaml\" data-type=\"URL\" data-id=\"https:\/\/github.com\/abiosoft\/caddy-yaml\">yaml<\/a>\u3001<a href=\"https:\/\/github.com\/awoodbeck\/caddy-toml-adapter\" data-type=\"URL\" data-id=\"https:\/\/github.com\/awoodbeck\/caddy-toml-adapter\">toml<\/a>\u3002\u914d\u7f6e\u52a0\u8f7d\u8fdb caddy \u540e\uff0c\u90fd\u8f6c\u6362\u4e3a json \u683c\u5f0f\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5199\u4e00\u4e2a Caddyfile\uff0c\u5185\u5bb9\u5982\u4e0b\uff0c\u5728\u542f\u52a8\u7684\u65f6\u5019\u52a0\u8f7d\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n:2015\nrespond &quot;\u4f60\u597d\uff0c\u6cd5\u5916\u72c2\u5f92\u5f20\u4e09&quot;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"252\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-2.png\" alt=\"\" class=\"wp-image-4533\"\/><\/figure>\n\n\n\n<p>\u7ba1\u7406\u7aef\u53e3\u662f 2019\uff0c\u670d\u52a1\u7aef\u53e3\u662f\u6211\u4eec\u6307\u5b9a\u7684 2015\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"447\" height=\"51\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-3.png\" alt=\"\" class=\"wp-image-4535\"\/><\/figure>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u5c06\u914d\u7f6e\u5199\u4e3a json \u683c\u5f0f\u6587\u4ef6\uff0c\u5728\u542f\u52a8\u65f6\u52a0\u8f7d\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  &quot;apps&quot;: {\n    &quot;http&quot;: {\n      &quot;servers&quot;: {\n        &quot;example&quot;: {\n          &quot;listen&quot;: &#x5B;&quot;:2015&quot;],\n          &quot;routes&quot;: &#x5B;\n            {\n              &quot;handle&quot;: &#x5B;{\n                &quot;handler&quot;: &quot;static_response&quot;,\n                &quot;body&quot;: &quot;Hello, world!&quot;\n              }]\n            }\n          ]\n        }\n      }\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"928\" height=\"274\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-4.png\" alt=\"\" class=\"wp-image-4540\"\/><\/figure>\n\n\n\n<p>caddy \u4e5f\u652f\u6301\u5410 metrics \u7ed9 Prometheus\u3002<\/p>\n\n\n\n<p>\u66f4\u8be6\u7ec6\u5b8c\u6574\u7684\u914d\u7f6e\u89c4\u5219\u89c1 <a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\">directives<\/a>\uff0c\u672c\u7bc7\u5c31\u4e0d\u8d58\u8ff0\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6587\u4ef6\u670d\u52a1<\/h2>\n\n\n\n<p>\u4f7f\u7528 <a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/file_server\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/file_server\">\u6587\u4ef6\u670d\u52a1<\/a>\u529f\u80fd\u65f6\uff0c\u5207\u6362\u5230\u6587\u4ef6\u670d\u52a1\u7684\u6839\u8def\u5f84\uff0c\u542f\u52a8 caddy\uff0c\u5982\u679c\u6ca1\u6709 index.html\uff0c\u53ef\u4ee5\u5e26\u4e0a &#8211;browse \u53c2\u6570\uff0c\u5728\u6d4f\u89c8\u5668\u91cc\u5c31\u80fd\u6d4f\u89c8\u6587\u4ef6\u4e86\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7 &#8211;root \u6765\u6307\u5b9a\u6587\u4ef6\u670d\u52a1\u7684\u6839\u8def\u5f84\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"926\" height=\"200\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-6.png\" alt=\"\" class=\"wp-image-4544\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1262\" height=\"417\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-5.png\" alt=\"\" class=\"wp-image-4542\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cd\u5411\u4ee3\u7406<\/h2>\n\n\n\n<p>\u518d\u770b\u770b\u7b2c\u4e8c\u4e2a\u6838\u5fc3\u529f\u80fd\u2014\u2014<a href=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy\" data-type=\"URL\" data-id=\"https:\/\/caddyserver.com\/docs\/caddyfile\/directives\/reverse_proxy\">\u53cd\u5411\u4ee3\u7406<\/a>\uff0c\u53cd\u5411\u4ee3\u7406\u7684\u914d\u7f6e\u6a21\u578b\u90fd\u5dee\u4e0d\u591a\u3002<\/p>\n\n\n\n<p>upstream \u652f\u6301\u5199 ip:port\u3001\u57df\u540d\u3001unix socket \u7b49\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8d1f\u8f7d\u5747\u8861\u65b9\u5f0f\u652f\u6301<ul><li>random<\/li><li>random_choose &lt;n&gt;\uff0c\u968f\u673a\u9009\u51fa n \u4e2a upstream\uff0c\u9009\u62e9\u8d1f\u8f7d\u6700\u5c0f\u7684 upstream \u8f6c\u53d1\u6d41\u91cf<\/li><li>first\uff0c\u6309\u914d\u7f6e\u987a\u5e8f\u67e5\u627e\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u53ef\u7528\u7684\u4f5c\u4e3a\u8f6c\u53d1\u7684 upstream<\/li><li>round_robin<\/li><li>least_conn<\/li><li>ip_hash<\/li><li>uri_hash<\/li><li>header [field]\uff0c\u6839\u636e\u5934\u90e8\u8fdb\u884c\u7c98\u6ede\u8f6c\u53d1\uff0c\u5982\u679c\u6ca1\u6709\u8be5\u5934\u90e8\u5b57\u6bb5\u5219\u968f\u673a\u8f6c\u53d1<\/li><li>cookie [&lt;name&gt; [&lt;secret&gt;]]<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>\u63d0\u4f9b\u4e24\u79cd\u5bf9 upstream \u7684\u5065\u5eb7\u68c0\u67e5\u65b9\u5f0f\uff1aactive\u3001passive\u3002active \u662f\u57fa\u4e8e\u5b9a\u65f6\u5668\u4e3b\u52a8\u8fdb\u884c\u5065\u5eb7\u68c0\u67e5\uff0cpassive \u662f\u6709\u5b9e\u9645\u6d41\u91cf\u53d1\u751f\u65f6\u624d\u987a\u5e26\u8fdb\u884c\u5065\u5eb7\u68c0\u67e5\u3002<\/p>\n\n\n\n<p>\u4e00\u4efd\u7c97\u7565\u7684\u53cd\u5411\u4ee3\u7406\u914d\u7f6e<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nreverse_proxy &#x5B;&amp;lt;matcher&gt;] &#x5B;&amp;lt;upstreams...&gt;] {\n\t# backends\n\tto      &amp;lt;upstreams...&gt;\n\tdynamic &amp;lt;module&gt; ...\n\n\t# load balancing\n\tlb_policy       &amp;lt;name&gt; &#x5B;&amp;lt;options...&gt;]\n\tlb_try_duration &amp;lt;duration&gt;\n\tlb_try_interval &amp;lt;interval&gt;\n\n\t# active health checking\n\thealth_uri      &amp;lt;uri&gt;\n\thealth_port     &amp;lt;port&gt;\n\thealth_interval &amp;lt;interval&gt;\n\thealth_timeout  &amp;lt;duration&gt;\n\thealth_status   &amp;lt;status&gt;\n\thealth_body     &amp;lt;regexp&gt;\n\thealth_headers {\n\t\t&amp;lt;field&gt; &#x5B;&amp;lt;values...&gt;]\n\t}\n\n\t# passive health checking\n\tfail_duration     &amp;lt;duration&gt;\n\tmax_fails         &amp;lt;num&gt;\n\tunhealthy_status  &amp;lt;status&gt;\n\tunhealthy_latency &amp;lt;duration&gt;\n\tunhealthy_request_count &amp;lt;num&gt;\n\n\t# streaming\n\tflush_interval &amp;lt;duration&gt;\n\tbuffer_requests\n\tbuffer_responses\n\tmax_buffer_size &amp;lt;size&gt;\n\n\t# request manipulation\n\ttrusted_proxies &#x5B;private_ranges] &amp;lt;ranges...&gt;\n\theader_up   &#x5B;+|-]&amp;lt;field&gt; &#x5B;&amp;lt;value|regexp&gt; &#x5B;&amp;lt;replacement&gt;]]\n\theader_down &#x5B;+|-]&amp;lt;field&gt; &#x5B;&amp;lt;value|regexp&gt; &#x5B;&amp;lt;replacement&gt;]]\n\tmethod &amp;lt;method&gt;\n\trewrite &amp;lt;to&gt;\n\n\t# round trip\n\ttransport &amp;lt;name&gt; {\n\t\t...\n\t}\n\n\t# optionally intercept responses from upstream\n\t@name {\n\t\tstatus &amp;lt;code...&gt;\n\t\theader &amp;lt;field&gt; &#x5B;&amp;lt;value&gt;]\n\t}\n\treplace_status &#x5B;&amp;lt;matcher&gt;] &amp;lt;status_code&gt;\n\thandle_response &#x5B;&amp;lt;matcher&gt;] {\n\t\t&amp;lt;directives...&gt;\n\n\t\t# special directives only available in handle_response\n\t\tcopy_response &#x5B;&amp;lt;matcher&gt;] &#x5B;&amp;lt;status&gt;] {\n\t\t\tstatus &amp;lt;status&gt;\n\t\t}\n\t\tcopy_response_headers &#x5B;&amp;lt;matcher&gt;] {\n\t\t\tinclude &amp;lt;fields...&gt;\n\t\t\texclude &amp;lt;fields...&gt;\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"937\" height=\"209\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-8.png\" alt=\"\" class=\"wp-image-4571\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"913\" height=\"402\" src=\"https:\/\/www.chenqing.work\/wp-content\/uploads\/2022\/09\/image-10.png\" alt=\"\" class=\"wp-image-4573\"\/><\/figure>\n\n\n\n<p>\u603b\u4f53\u6765\u8bf4\uff0c\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u5b98\u65b9\u6587\u6863\u7ec4\u7ec7\u5f97\u4e5f\u5f88\u8212\u670d\uff0c\u914d\u8272\u633a\u4e0d\u9519\u3002<\/p>\n\n\n\n<p>\u4e0b\u4e00\u6b21\u518d\u6765\u5b66\u4e60\u5b66\u4e60\u5b83\u7684\u67b6\u6784\ud83e\udd14<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u3001Rust \u8fd9\u4fe9\u8bed\u8a00\u706b\u8d77\u6765\u540e\uff0c\u53c8\u7206\u53d1\u4e86\u4e00\u6ce2\u9020\u8f6e\u5b50\u7684\u70ed\u6f6e\u3002Go \u6709\u5927\u540d\u9f0e\u9f0e\u7684 Kubernetes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[92,94,93],"class_list":["post-4455","post","type-post","status-publish","format-standard","hentry","category-code","tag-caddy","tag-web","tag-93"],"_links":{"self":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4455"}],"version-history":[{"count":76,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4455\/revisions"}],"predecessor-version":[{"id":4583,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/4455\/revisions\/4583"}],"wp:attachment":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}