跳到主要內容

支援 HTTP/3 網頁伺服器 Caddy v2 安裝

自已原本擅長使用的網頁伺服器是 Apache2,還在學校的時候已經能夠使用 Let's Encypt 的 SSL 憑證來達成 http 2.0 的服務。

除了 Apache 外,也嚐試使用過 Nginx 及 Lighttp 等網頁伺服器程式,相較下 Apache 穩定但花費效能較多,但是因為比較熟悉順手,來到基隆時一樣用 Apache 來架設網頁。

但在 http/3 規格確定後,許多大型網站,如:Google、Facebook等都採用 http/3 服務。http/3 採用 udp 通訊協定,理論上可以加快網頁的傳輸速度,網路上的資料說 Apache 尚未有改版支援 http/3 的消息,加上想要試著改掉古老又笨重的 apache 伺服器,找了幾個目前有支援或打算支援 http/3 的網頁伺服器。

目前找到的伺服器有:Caddy、OpenLiteSpeed、Nginx(開始改版支援中)、新版的 IIS。

  • Caddy:用 go 語言撰寫的網頁伺服器,可用於作代理。除了支援 http/3 外,如果有合法的網域 DNS,甚至會自動產生 Let's Encypt SSL 憑證,完全不需要經過申請。Caddy 2 (caddyserver.com)
  • OpenLiteSpeed:具有 cPanel的後台管理系統,但是試用了一下不太會用。OpenLiteSpeed

目前實作是用 Caddy v2。




安裝

Install — Caddy Documentation (caddyserver.com)

Caddy2 有提供各大 linux 版本套件,以自已是順手的 ubuntu 為例:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg

curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

sudo apt update

sudo apt install caddy

安裝後,Caddy2 會自動啟動,基本上不用作任何設定,http:80 port 應該就已經啟用,直接用瀏覽器上 http://localhost/ 或 http://127.0.0.1/ 就可以看到預設的啟動畫面。


設定

Caddy2 的設定檔位置:/etc/caddy/Caddyfile

中文的資料很少,經過好幾天查看官網的說明才把設定差不多可以搞懂,可以初步設定想要的功能。

要使用 http/3 及 Automatic HTTPS需要滿足幾個條件:

  1. 要有合法的DNS:
    最近把一台 Pi4 重新安裝 ubuntu server,並且在 no-ip 申請一個 ddns:etuser2.hopto.org,確定 DDNS 已經生效後,把 DNS 設定到 Caddyfile 中。哇啦!Let's Encypt 的 SSL 就自動產生了。







  2. 因為 http/3 走的是 udp 通訊協定,所以在 NAT 或防火牆需要設定 udp/443 的通道。

左上角的橘色閃電的圖示表示 http/3 有啟動。


用 http/3 checker 網站來測試,結果發現有支援 http/3,但未支援 QUIC。查看官方論譠,發現官方認為不值得支援 QUIC。


PHP 設定

(enable_php) {

        php_fastcgi unix//run/php/php-fpm.sock {

                # index index.php

        }

        #encode zstd gzip

       #不要開啟 http compressiong,有安全疑慮

}

Header 設定(安全性)

(security) {

        header {

        # enable HSTS

        Strict-Transport-Security "max-age=31536000; includeSubDomains"


        # disable clients from sniffing the media type

        X-Content-Type-Options “nosniff”


        # clickjacking protection

        X-Frame-Options “SAMEORIGIN”        


        # Enable cross-site filter (XSS) and tell browser to block detected attacks

        X-Xss-Protection "1; mode=block"

        X-Permitted-Cross-Domain-Policies: "none"


        # keep referrer data off of HTTP connections

        Referrer-Policy "strict-origin-when-cross-origin"


        # Content Security Policy

        Content-Security-Policy "default-src 'self' https://fonts.googleapis.com https://fonts.gstatic.com https://ajax.googleapis.com;object-src 'self'; base-uri 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://connect.facebook.net https://polyfill.io https://cdn.ckeditor.com https://ckeditor.iframe.ly https://use.fontawesome.com https://cdn.jsdelivr.net https://ajax.googleapis.com https://www.google.com https://www.gstatic.com https://cdnjs.cloudflare.com https://code.jquery.com https://www.googletagmanager.com https://unpkg.com; style-src 'self' 'unsafe-inline' https://maxcdn.bootstrapcdn.com https://fonts.googleapis.com https://cdn.ckeditor.com https://cdn.jsdelivr.net https://use.fontawesome.com https://unpkg.com; img-src 'self' data: https://* https://king.kl.edu.tw https://chart.googleapis.com https://cdn.ckeditor.com https://*.googleusercontent.com; font-src 'self' https://maxcdn.bootstrapcdn.com https://use.fontawesome.com https://fonts.gstatic.com https://cdn.jsdelivr.net data:; frame-src 'self' https://www.youtube.com https://www.google.com https://www.facebook.com https://drive.google.com; frame-ancestors 'self';"


        Permissions-Policy "geolocation=(),midi=(),sync-xhr=(),microphone=(self),camera=(self),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()"


        -Server Caddy

        }

}

靜態檔案暫存設定

(static) {

        @static {

                file

                path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *.json *.webp *.ttf *.pdf

        }

        header @static Cache-Control max-age=5184000

}

避免敏感檔案暴露

        respond /.* 404
        respond /builds* 404
        respond /env* 404
        respond /composer.* 404
        respond /*.lock 404
        respond /*.md 404
        respond /*.json 404
        respond /*.dist 404

開啟壓縮

encode zstd gzip

Log 設定

log {

                output file /var/log/caddy/access.log {

                        roll_size 10mb

                        roll_keep 3

                        roll_keep_for 720h

                }

        }

Snippets

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

https://digi.freshman.com.tw/caddy/caddy-snippets/

https://caddy.community/c/wiki/13






留言

這個網誌中的熱門文章

IPv6系統(三):伺服器手動設定 & Fortigate SLAAC 設定

一、伺服器手動設定 機房的伺服分為 Ubuntu 及 Windows Server 系列兩種。Windows Server 具有圖形介面(GUI),且與 Win7、Win10 作業系統操作相似,相當容易設定。只要把 IP 相關資訊輸入即可。 Ubuntu linux 系統需要修改系統設定檔(/etc/network/interface) 二、Fortigate SLAAC 設定 以學校中 vlan192 為例,IPv6區段為 2001:288:102b:a5::/64 最主要是 config ipv6 之後的區段,set ip6-address 設定本機作為這個 vlan 的 IPv6 的 IP,set ip6-send-adv、set ip6-other-flag 開啟RA功能,並設定成 DHCP+SLAAC 動態 IPv6 環境。 因為採用 DHCP+SLAAC 的緣故,必須要有 DHCPv6 來發放 DNS 資訊,否則要每一台手動設定才能正確連接網路。 除此之外,Fortigate 上還需要針對 IPv6 做 Routing table 和 IPv6 Firewall Rule 的設定。

IPv6 系列(五):KEA DHCP SERVER

學校在純 IPv4 的環境時,採用 ISC-DHCP Server 來發放教學區域的私有 IP(10.10.xxx.xxx),並使用閘道 Fortigate 作 NAT 連接到 Internet。 採用 IPv6 後,原本是要使用 ISC-DHCPv6 來作為 IPv6 發放,結果 ISC-DHCPv6 架不起來。不過在查詢 DHCPv6 的過程中,發現 ISC(Internet System Consortium)已開發出下一代的 DHCP Server:Kea Dhcp Server。 Kea Dhcp Server 官網: https://www.isc.org/kea/ 摘錄  https://www.itread01.com/content/1548712273.html  對 Kea DHCP Server 的介紹: Kea DHCP Server 由 Internet Systems Consortium(ISC) 開發的開源 DHCPv4/DHCPv6 伺服器。Kea DHCP Server 是一個高效能的,可擴充套件的DHCP伺服器引擎。通過hooks library可以很容易的修改和擴充套件。 Kea包含的功能元件 keactrl — kea伺服器啟動,停止,配置重置和狀態查詢元件 kea-dhcp4 — DHCPv4伺服器程式,用於響應客戶端的DHCPv4查詢。 kea-dhcp6 — DHCPv6伺服器程式,用於響應客戶端的DHCPv6查詢。 kea-dhcp-ddns — DHCP動態DNS程式.這個程式在DHCP伺服器和DNS伺服器之間扮演者調解者。它接收來自DHCP伺服器域名更新請求和傳送DNS更新訊息給DNS伺服器。也就是說DDNS捕獲使用者每次變化的IP地址,然後將其與域名相對應,這樣其他上網使用者就可以通過域名來進行交流。而最終客戶所要記憶的全部,就是記住動態域名商給予的域名即可,而不用去管他們是如何實現的。 軟體執行環境 加密庫Botan或者OpenSSL log4cplus 1.0.3+ 如果有需要mysql,需要MySQL headers and libraries。可選(postgresql、cassandra類同) 選用 Kea DHCP Server 的原因,第一,提供 DHC

幾個 Linux command

搜尋 n 天前有修改的檔案 find -mtime -n 刪除 n 天前建立的檔案 find -mtime +n -exec rm {} \; 以檔名規則搜尋檔案並刪除 find . -name "abc*" -delete 同步排除某些資料夾或檔案 rsync -r -q --exclude "dir*" --exclude "file*.txt" [source] [distination]