简单粗暴的Caddy Server 使用介绍
Caddy是一个Go写的服务器软件,官方的宣传语“The HTTP/2 web server with automatic HTTPS”以及“Serve The Web Like It's 2016”简明表达了这个软件的优点和趋势,它拥有基本的apache或者nginx有的web server模块,同时还有一些很有特色的功能,比如:
HTTP/2
Automatic HTTPS
Multi-core
Websockets
Markdown
IPv6
Git
…...
用Caddy我们就可以很方便的部署一个Markdown文本作为静态网站访问,或者它的Git指令完成代码的自动化部署,当然它很大的特色就是它的语法非常简洁,比nginx还要简单,配置部署起来很方便,下面随便举几个例子吧。
对网站添加BasicAuth,用户名ryan,密码 12345
basicauth / ryan 12345
用CORS解决跨域问题
cors / {
origin http://allowedSite.com
origin http://anotherSite.org https://anotherSite.org
methods POST,PUT
allow_credentials false
max_age 3600
allowed_headers X-Custom-Header,X-Foobar
exposed_headers X-Something-Special,SomethingElse
}
IP过滤
ipfilter / {
rule block
ip 212.10.15.0-255 213.10.15.0-10 5.23.4.24
blockpage /local/data/default.html
}
HTTPS 配置
tls ../cert.pem ../key.pem
实在太简单了配置起来,具体还有其他简洁到哭的指令可以看官方的User Guide,很快就刷完了。
既然Caddy自动部署https,而且是通过Let's Encrypt,那么就实践一下,本博客是用Ghost 搭建,Nginx代理的,现在就改用Caddy,并支持https,步骤大体如下:
证书申请请移步 Let's Encrypt
Caddy 安装很简单,直接下载 https://caddyserver.com/docs/getting-started
配置Caddyfile,看起来指令简洁明了
https://www.yuansir-web.com, http://www.yuansir-web.com, http://yuansir-web.com { redir https://yuansir-web.com{uri} tls yuansir88@gmail.com } https://yuansir-web.com { gzip errors { log /var/log/caddy/yuansir-web.error.log { size 50 age 30 keep 5 } } log /var/log/caddy/yuansir-web.access.log tls yuansir88@gmail.com proxy / http://127.0.0.1:2368 { proxy_header X-Real-IP {remote} proxy_header HOST {host} proxy_header X-Forwarded-Proto {scheme} } }
用supservisor来管理Caddy运行
[program:caddy] command=/usr/bin/caddy -conf="/var/www/Caddyfile" directory=/var/www ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) autorestart=unexpected ; whether/when to restart (default: unexpected) startsecs=1 ; number of secs prog must stay running (def. 1) startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) stopasgroup=false ; send stop signal to the UNIX process group (default false) user=www ; setuid to this UNIX account to run the program redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile=/var/log/caddy.log ; stdout log path, NONE for none; default AUTO stderr_logfile=/var/log/caddyerr.log ; stderr log path, NONE for none; default AUTO
将网站的静态资源CDN换成支持https的CDN
好了,就这么简单,Nginx切换成Caddy,并支持https了,真是多快好省。