1、caddy 简介
Caddy是一款功能强大,扩展性高的Web服务器,目前在Github上已有38K+Star。Caddy采用Go语言编写,可用于静态资源托管和反向代理。
官方文档地址 caddyserver.com/docs/
Caddy具有如下主要特性:
- 对比Nginx复杂的配置,其独创的Caddyfile配置非常简单;
- 可以通过其提供的Admin API实现动态修改配置;
- 默认支持自动化HTTPS配置,能自动申请HTTPS证书并进行配置;
- 能够扩展到数以万计的站点;
- 可以在任意地方执行,没有额外的依赖;
- 采用Go语言编写,内存安全更有保证。
2、安装 caddy
安装说明地址 caddyserver.com/docs/instal…
centos7 安装方法如下:
yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy
3、常用场景
3.1 静态文件服务
Caddyfile 配置文件如下:
1、文件服务
:10100
{
root * /opt/
file_server
}
启动应用程序
caddy run --config /root/caddy/Caddyfile --watch
访问地址
2、文件预览服务
:10100
{
root * /opt/
file_server browse
}
访问地址,页面展示如下:
3.2 反向代理
:10100
{
root * /opt/
reverse_proxy /community/* eco.dameng.com
file_server browse
}
reverse_proxy /community/* eco.dameng.com
所有 /community/* 的请求都代理到 eco.dameng.com 服务器上
访问
http://119.96.92.173:10100/community/question/0cb6d448f1fd97f04f1bf682077f3ebd
跳转到
3.3 重定向
:10100
{
root * /opt/
reverse_proxy /community/* eco.dameng.com
redir /info/* https://eco.dameng.com{uri}
file_server browse
}
redir /info/* eco.dameng.com{uri}
所有 /info/* 请求都重定向导 所有 /info/* 请求都重定向到 eco.dameng.com 网站
3.4 重写
:10100
{
root * /opt/
reverse_proxy /community/* eco.dameng.com
redir /info/* https://eco.dameng.com{uri}
rewrite /test1.html /test.html
file_server browse
}
rewrite /test1.html /test.html
请求 /test1.html 重写到 /test.html 不改变浏览器的地址
3.5 单页面应用程序
If your SPA is coupled with an API or other server-side-only endpoints, you will want to use handle blocks to treat them exclusively:
example.com {
encode gzip
handle /api/* {
reverse_proxy backend:8000
}
handle {
root * /path/to/site
try_files {path} /index.html
file_server
}
}
3.6 访问的日志文件配置
{
debug
log {
output file /var/log/access.log
}
}
:10100
{
root * /opt/
reverse_proxy /community/* eco.dameng.com
redir /info/* https://eco.dameng.com{uri}
rewrite /test1.html /test.html
file_server browse
}
在全局配置中进行配置。