簡單粗暴的 Caddy Server

Ryan發表於2016-05-21

本文最早發表於本人部落格: 簡單粗暴的Caddy Server

在上一篇的《Mac極簡的開發環境Laravel Valet實踐》中提到Valet是利用系統後臺啟用的Caddy來提供web服務的,這個Caddy是個web 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,步驟大體如下:

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了,真是多快好省。

轉載請註明: 轉載自Ryan是菜鳥 | LNMP技術棧筆記

如果覺得本篇文章對您十分有益,何不 打賞一下

謝謝打賞

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章