简略粗犷的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了,真是多快好省。