caddy安装和简单配置说明

caddy安装|caddy简单配置说明

caddy

caddy 官网霸气概述:[终极服务]使您的网站比任何其他解决方案都更安全、更可靠、更具可扩展性。

caddyserver 官网链接

caddyserver 安装参考文档链接

Ubuntu 可以试着通过命令安装包一键安装, centos7 通过静态二进制包方式安装。

caddy 静态二进制文件安装参考文档

caddy 二进制包下载链接

解压安装包并安装

1
2
tar -zxf caddy_2.9.1_linux_amd64.tar.gz
mv caddy /usr/local/bin/

查看安装版本

1
caddy -v

v2.9.1 h1:OEYiZ7DbCzAWVb6TNEkjRcSCRGHVoZsJinoDR/n9oaY=

添加 caddy 系统用户组和用户

1
2
3
4
5
6
7
8
9
groupadd --system caddy

useradd --system \
    --gid caddy \
    --create-home \
    --home-dir /var/lib/caddy \
    --shell /usr/sbin/nologin \
    --comment "Caddy web server" \
    caddy

配置 systemed 服务,caddy.service 服务配置文件/usr/lib/systemd/system/caddy.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

加载 caddy.service

1
2
3
sudo systemctl daemon-reload
#  开机自启
sudo systemctl enable --now caddy

验证 caddy 服务运行状态

1
systemctl status caddy

默认 caddy 启动配置文件 /etc/caddy/Caddy 初始化默认配置

1
2
mkdir -p /etc/caddy
touch /etc/caddy/Caddy
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace ":80" below with your
# domain name.

:80 {
	# Set this path to your site's directory.
	root * /usr/share/caddy

	# Enable the static file server.
	file_server

	# Another common task is to set up a reverse proxy:
	# reverse_proxy localhost:8080

	# Or serve a PHP site through php-fpm:
	# php_fastcgi localhost:9000
}

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
example.com www.example.com {

  root * /var/www/html
  
  file_server
  encode zstd gzip
  header {
    ?Cache-Control "max-age=1800"
  }
  log {
    output file /var/lib/caddy/access_log.log
  }
  
  handle /files/* {
    uri strip_prefix /files
    root /data/images
    header {
      ?Cache-Control "max-age=1800"
    }
    encode /* {
      gzip
      zstd
      match {
         header Content-Type text/*
         header Content-Type image/*
      }
    }
  }
}

proxy.nihility.cn {

  reverse_proxy /* http://api.example.com

  file_server
  encode zstd gzip
  header {
    ?Cache-Control "max-age=1800"
  }
  log {
    output file /var/lib/caddy/api_access_log.log
  }

}

相关内容