簡單粗暴的Caddy Server 使用介紹
Caddy是一個Go寫的服務器軟件,官方的宣傳語“The HTTP/2 web server with automatic HTTPS”以及“Serve The Web Like It's 2016”簡明表達了這個軟件的優(yōu)點和趨勢,它擁有基本的apache或者nginx有的web server模塊,同時還有一些很有特色的功能,比如:
HTTP/2
Automatic HTTPS
Multi-core
Websockets
Markdown
IPv6
Git
…...
用Caddy我們就可以很方便的部署一個Markdown文本作為靜態(tài)網(wǎng)站訪問,或者它的Git指令完成代碼的自動化部署,當然它很大的特色就是它的語法非常簡潔,比nginx還要簡單,配置部署起來很方便,下面隨便舉幾個例子吧。
對網(wǎng)站添加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代理的,現(xiàn)在就改用Caddy,并支持https,步驟大體如下:
證書申請請移步
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
將網(wǎng)站的靜態(tài)資源CDN換成支持https的CDN
好了,就這么簡單,Nginx切換成Caddy,并支持https了,真是多快好省。
相關文章
用rsync實現(xiàn)windows與linux文件同步的方法
windows做為文件服務器,使用rsync的windows服務版本,然后配置好就可以了。需要的朋友可以參考下。2011-02-02生產(chǎn)服務器突然本機無法訪問本機IP的端口的問題及解決方法
生產(chǎn)服務器突然無法訪問自己本機IP地址的端口,通過localhost或者127.0.0.1都可以正常訪問,本文給大家分享生產(chǎn)服務器突然本機無法訪問本機IP的端口的問題及解決方法,感興趣的朋友一起看看吧2023-11-11Deployment副本無狀態(tài)服務創(chuàng)建及水平擴展
這篇文章主要為大家介紹了Deployment副本無狀態(tài)服務及水平擴展,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03