简单粗暴的Caddy Server 使用介绍
技术  /  管理员 发布于 7年前   235
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 [email protected]}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 [email protected] 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 programredirect_stderr=true ; redirect proc stderr to stdout (default false)stdout_logfile=/var/log/caddy.log ; stdout log path, NONE for none; default AUTOstderr_logfile=/var/log/caddyerr.log ; stderr log path, NONE for none; default AUTO
将网站的静态资源CDN换成支持https的CDN
好了,就这么简单,Nginx切换成Caddy,并支持https了,真是多快好省。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号