小知识:简单粗暴的Caddy Server 使用介绍

Caddy是一个Go写的服务器软件,官方的宣传语“The HTTP/2 web server with automatic HTTPS”以及“Serve The Web Like Its 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,而且是通过Lets Encrypt,那么就实践一下,本博客是用Ghost 搭建,Nginx代理的,现在就改用Caddy,并支持https,步骤大体如下:

证书申请请移步 Lets Encrypt Caddy 安装很简单,直接下载 https://caddyserver.com/docs/getting-started 配置Caddyfile,看起来指令简洁明了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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了,真是多快好省。

原文链接:https://yuansir-web.com/2016/05/21/jian-dan-cu-bao-de-caddy-server/

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:centos6.4+nginx+mysql+php+phpmyadmin整合过程详解

2023-4-26 16:20:15

建站知识

小知识:SVN无法提交出现 Cant set file "/db/txn_current" read-write :拒绝访问

2023-4-26 16:34:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索