0%

Caddy 部署 SPA

caddy

macOS 下可以 brew install caddy, 当前 v2 体验不错

old nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 1234;

# api
location ^~ /api {
proxy_pass http://127.0.0.1:1235;
}

location / {
try_files $uri /index.html;
add_header Cache-Control max-age=2592000;
alias /your-spa-project/dist/;
}

location = /index.html {
alias /your-spa-project/dist/index.html;
add_header Cache-Control no-cache;
}
}

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
#
# global options
#
{
auto_https off
}

:1234 {
encode zstd gzip

handle /api/* {
reverse_proxy 127.0.0.1:1235
}

handle {
root * /your-spa-project/dist/
try_files {path} /index.html

handle /index.html {
header Cache-Control no-cache
file_server
}

handle {
header Cache-Control max-age=2592000
file_server
}
}
}

caddy 使用体验

DX

VSCode Extension https://marketplace.visualstudio.com/items?itemName=matthewpi.caddyfile-support

caddy & nginx

proxy_pass / reverse_proxy

nginx proxy_pass

proxy_pass 比较 trick, proxy_pass http://127.0.0.1:1235;proxy_pass http://127.0.0.1:1235/; 结果是不一样的, ending slash 会 trim prefix

caddy reverse_proxy

caddy 可以使用 handle_path 去 trim prefix, 更直观

reload config

  • nginx: edit .conf file, nginx -s reload
  • caddy: 可以使用 caddy run --watch, watch Caddyfile 的变化自动 reload

brew services

在 macOS 中均可以用 brew services 来管理, 配置文件

  • /usr/local/etc/nginx.conf
  • /usr/local/etc/Caddyfile

handle / handle_path / route

  • route 内部不会对 directives 重新排序
  • handle 互斥, 更好的隔离, 可以嵌套