如果你不想要某些網域被網路爬蟲抓到,但是不想更改網站上應用程式的話,除了從 nginx 限制 IP 或是 base-auth 之外,也可以從 cloudfalre 下手。當然前提是你的網域必需要經過 cloudlfare 的邊際網絡。
這裡介紹可以使用 Cloudfalre 的 WAF 功能。

進入 WAF 頁面後,點擊建立規則。

點擊,編輯運算式,輸入以的 user agent 條件。
(cf.client.bot)
or (http.user_agent contains "Googlebot")
or (http.user_agent contains "Bingbot")
or (http.user_agent contains "bing")
or (http.user_agent contains "Slurp")
or (http.user_agent contains "DuckDuckBot")
or (http.user_agent contains "Baiduspider")
or (http.user_agent contains "YandexBot")
or (http.user_agent contains "sogou")
or (http.user_agent contains "Spider")
or (http.user_agent contains "Exabot")
or (http.user_agent contains "facebot")
or (http.user_agent contains "PetalBot")
or (http.user_agent contains "SEMrushBot")
or (http.user_agent contains "Majestic")
or (http.user_agent contains "DotBot")
or (http.user_agent contains "AhrefsBot")
這條件應該還可以再添加,但是規則有總字符數上限。上面列出的是常見的爬蟲包含名稱。同樣的,如果你想要封鎖你不想要的爬蟲名稱也可以再上面設定,不過 Cloudflare 其實有一個「機器人對抗模式」的功能,啟用後他會封鎖壞機器人。 WAF 免費只能有 5 個規則,使用上還是得注意一下喔!
再來規則就選擇封鎖,然後部署。

再來用 postman 更改 useragent 測試看看,確認沒問題就行啦!以上。