Caddy 幾個好用的 snippets

1. 什麼是 snippet?

Snippet 是使用者可以自行定義的代碼片段,設定後可以在 Caddyfile 中需要的地方重複使用

Caddy 的官網有 Caddyfile 的圖解,您可以觀察一下 snippet 在哪裏 


Caddyfile 圖解

你可以用括號自行定義 snippet 的名稱


(yoursnippet) {
    你的代碼片段
}

然後用 import yoursnippet 重複加入到你任何想使用的地方

2. 實作一:運用瀏覽器緩存(Add Expires headers)

設定檔案的過期時間,可以減少伺服器的負載和頁面載入的時間,有很高的成本效益比。

讓我們來看看一個實作的例子


(static) {
	@static {
		file
		path *.ico *.css *.js *.gif *.jpg *.jpeg *.png * .svg *.woff *.woff2 *.json *.webp
	}
	header @static Cache-Control max-age=5184000
}
你的網站 {
    ...
    import static
    ...
}

首先用(static)宣告一個 snippet
然後用 @static 宣告一個 named machters
用 file 找出有檔案存在的路徑
用 path 找出包含這些副檔名的檔案
最後用 header 來宣告符合 @static 這個 matchers 的檔案快取的過期時間

3. 實作二:HTTP強制安全傳輸技術(HSTS)

我們以為安全的網路,其實很容易被駭客劫持,當你在瀏覽網站時,有可能被駭客插入虛假的網頁資料。

網站如果提供 HTTP 強制安全傳輸技術(HSTS),可以避免中間人攻擊和連線劫持,能保護使用者的安全。

這裡提供一個實例給大家參考


(security) {
	header {
        # enable HSTS
        Strict-Transport-Security max-age=31536000;
        # disable clients from sniffing the media type
        X-Content-Type-Options nosniff
        # clickjacking protection
        X-Frame-Options SAMEORIGIN
        # keep referrer data off of HTTP connections
        Referrer-Policy no-referrer-when-downgrade
        # Content Security Policy
        Content-Security-Policy "base-uri 'self'; default-src 'self' 'unsafe-inline' 'unsafe-eval' https: data:"
	}
}
你的網站 {
    ...
    import security
    ...
}

Content Security Policy 的部分比較複雜,這裡只提供最基本的設定,你可以酌量增加適合你網站的設定。


實作一和二的參考網址

4. 實作三:撰寫快取服務的 snippet

Wordpres 有許多不錯的快取外掛,如果要有最佳的效能,需要在 server 端寫一些 snippet。

以下是比較冷門快取外掛 WP Performance 的實作


(wppcache) {
        header Vary Accept-Encoding
        @is-gzip {
                file
                path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
        }
        header @is-gzip {
            Content-Type text/html
            Content-Encoding gzip
        }
        @no-gzip {
                file
                not path *.html_gz *.html_mobile_gz *.html_amp_gz *.html_mobile_amp_gz
        }
        @mobile header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|OperasMini|OperasMini
        @amp path /amp/*
        @gzip header_regexp Accept-Encoding gzip        
        @mobile-gzip {
                header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|OperasMini|OperasMini
                header_regexp Accept-Encoding gzip
        }
        @amp-gzip {
                path /amp/*
                header_regexp Accept-Encoding gzip
        }
        @mobile-amp-gzip {
                header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|OperasMini|OperasMini
                path /amp/*
                header_regexp Accept-Encoding gzip
        }       
        @else {
                not header_regexp User-Agent Mobile|Android|Kindle|BlackBerry|OperasMini|OperasMini
                not path /amp/*
                not header_regexp Accept-Encoding gzip
        }
        @cache {
                not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
                not path_regexp "(/wp-json/|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
                not method POST
                not expression {query} != ''
    }
    route @cache {
                handle @mobile {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_mobile
                }
                handle @amp {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_amp
                }
                handle @gzip {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_gz
                }
                handle @mobile-gzip {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_mobile_gz
                }
                handle @amp-gzip {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_amp_gz
                }
                handle @mobile-amp-gzip {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?_mobile_amp_gz          
                }
                handle @else {
                        try_files /wp-content/cache/wpp-cache/{host}{uri}/index.html {path} {path}/index.php?{query}
                }
    }
}
你的網站 {
    ...
    import wppcache
    ...
}

Cache Enabler, WP Super Cache, WP-Rocket, WP Fastest cache, W3 Total Cache 等熱門快取外掛的使用者有福了,已經有人寫好了範例如下:


快取外掛範例

Caddy 的 Wiki 提供了許多實用的範例,值得參考看看:


Wiki for Caddy

相關文章