簡單粗暴的Caddy Server

本文最先發表於本人博客: 簡單粗暴的Caddy Serverhtml

在上一篇的《Mac極簡的開發環境Laravel Valet實踐》中提到Valet是利用系統後臺啓用的Caddy來提供web服務的,這個Caddy是個web server?今天玩了一下,很是贊,固然簡單粗暴是有點標題黨了。nginx

Caddy是一個Go寫的服務器軟件,官方的宣傳語「The HTTP/2 web server with automatic HTTPS」以及「Serve The Web Like It's 2016」簡明表達了這個軟件的優勢和趨勢,它擁有基本的apache或者nginx有的web server模塊,同時還有一些頗有特點的功能,好比:laravel

  • HTTP/2
  • Automatic HTTPS
  • Multi-core
  • Websockets
  • Markdown
  • IPv6
  • Git
  • …...

用Caddy咱們就能夠很方便的部署一個Markdown文本做爲靜態網站訪問,或者它的Git指令完成代碼的自動化部署,固然它很大的特點就是它的語法很是簡潔,比nginx還要簡單,配置部署起來很方便,下面隨便舉幾個例子吧。web

  • 對網站添加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,很快就刷完了。apache

既然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技術棧筆記cors

若是以爲本篇文章對您十分有益,何不 打賞一下socket

謝謝打賞

相關文章
相關標籤/搜索