使用cloudflare
介紹
他會幫你在你的網站server前再加一道防護,你可以去你的DNS provider中把ns(name server)改成cloudflare提供的兩個位置即可
加完了以後再回到cloudflare點選右邊藍色的Recheck按鈕即可。
加上A紀錄。
之後要等一小時左右,讓NameServer 生效。
使用
照著官網步驟即可,但注意在搜尋DNS設定時可能有些不會找到,要自己加上去
設定SSL
產生 origin certificate 即可,client certificate 可以不用,然後如果 server 是放在自己 host 的就選擇 full,放在 github 之類的選擇 Flexible
https://support.cloudflare.com/hc/en-us/articles/200170416-What-do-the-SSL-options-mean-
進入dashboard後點選Crypto之後ssl有三個選項
以nginx為例,如果使用Full則必須要加上以下才可以,不然監聽443 port時會無法存取網頁。
其中pem與key是從Crypto頁下方的
Origin Certificates
產生。
如果用Flexible策略則在nginx不用加入以上。
官網說明
配置後要等一下才會生效(可能到幾個小時),如果是讓github page 自訂域名後用https,設定要選Flexible
用 Full SSL
之後到 nginx 設定如下:
用 Flexible SSL
如果用nginx,則在nginx設置好 80 port 後在https網址即可看到
因為 flexible 是由 cloudflare 設置 client 與 cloudflare 間的 https 連線,而 cloudflare 與 api server 或 web server 之間仍是走 http,所以可以不用在 server 安裝證書,且 nginx 監聽 80 port 即可。
包含後端與前端
cloudflare 設置
Nginx 範例 config
產生 redirect loop Error
如果設定為
flexible SSL
打開網頁一直顯示 redirect error 可參考:大意為因爲我們在nginx 加上 redirect 80 to 443 所以造成此問題。
所以如果 nginx 有寫 redirect 的話必須用 Full SSL。
使用cloudflare後Nginx在配置Https不用監聽著443
都監聽著80即可。
使用nginx搭配cloudflare配置https(Origin certificate)
先前是指在browser到cloudflare有加密SSL 這個是在cloudflare到你的server這段也加上SSL
https://support.cloudflare.com/hc/en-us/articles/217471977
先到https://www.cloudflare.com/a/crypto
然後往下拉點選Create certificate`
之後會給你pem跟key,複製起來存到主機中
並且在 cloudflare dns 加上 A 記錄,輸入 api subdomain 名稱
之後加nginx的config加上
在 browser 如果直接輸入 API 位置的話會出現 untrusted certificate 是正常的(只要看到 certificate 的到期日是符合 Cloudflare 上的即可 ,可以直接發請求給 HTTPS 的 Endpoint 沒問題。
目前範例配置
Origin Certificate是我們想在Server上安裝時需要申請的,而Edge Certificate是cloudflare自動產生的,不用理會。
其中pem與key,pem下次點選網頁上Download按鈕還可以看到,但Key下一次點進來就看不到了。
注意:
有時使用Github page設定subdomain域名但還是找不到,換一個subdomain即可。
目前測試如果放兩個subdomain則會無法進入https://
E.g.
在Github page加上apex domain
(apex domain就是沒有subdomain的domain)
在cloudflare的A記錄加上Github給的四個A記錄即可
轉導域名到subdomain
點選 Page Rules
Github pages 強制使用 HTTPS URL
原本在 github pages 的 enforce HTTPS 會無法勾選,需要到 cloudflare 的 page rules 設置。
Last updated