in , ,

cPanel 找出壞 BOT!阻擋無用流量避免主機負載飆高的完整教學

快速掌握 BOT 流量來源,並用 .htaccess 有效降低主機負荷


壞 BOT 為什麼會吃掉流量、拖累主機負載?

如果您發現您的流量被很多不知名的 BOT 吃掉了
cPanel 虛擬主機控制台可以查看,位置在登入 cPanel 控制台→Awstats→Robots/Spiders visitors (Top 25) 。

壞 BOT(Bad Bots)會造成:

  • 流量被大量消耗
  • 主機 CPU 長時間維持高負載
  • 記憶體(RAM)被耗盡,使 PHP 無法正常執行
  • MySQL 查詢暴增,造成資料庫延遲
  • 網站速度變慢或出現 503、508 錯誤

在 cPanel Awstats 查看 BOT 流量來源

在 cPanel 中使用 Awstats,可以看到網站的詳細訪客與 BOT 請求紀錄:

  1. 登入 cPanel
  2. 點選 Awstats
  3. 選擇要查看的網域
  4. 檢查 Robots/Spiders visitors 與 Hits 是否異常偏高

如果看到不熟悉的 BOT 名稱、短時間大量造訪,通常就是主機負載升高的原因之一。

.htaccess vs robots.txt 的差異與使用情境

許多使用者誤以為 robots.txt 就能阻擋 BOT,但兩者完全不同:

項目 .htaccess robots.txt
作用層級 Apache 層(伺服器端) 協議層(指引性建議)
阻擋能力 可真正阻擋連線 無法阻擋,只是告知
對象 所有 BOT 與瀏覽器 僅「守規矩」的好 BOT
是否能防惡意 BOT 部分能,但可被繞過(見下節) 幾乎無效,壞 BOT 不會遵守
常見用途 封鎖壞 BOT、阻擋路徑、流量控管 告知搜尋引擎哪些能抓、不能抓

結論: robots.txt 並不是安全手段,而是「指南」。要阻擋 BOT 必須使用 .htaccess。

User-Agent 是寫入 .htaccess 的核心依據

BOT 在訪問網站時會傳送一段身份標示字串,稱為 User-Agent
例:

Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

在 .htaccess 中封鎖 BOT,就是依照 User-Agent 字串比對,例如:

BrowserMatchNoCase Baiduspider bad_bot

代表只要 User-Agent 中包含「Baiduspider」,就會被視為 bad_bot 並阻擋。

為什麼 .htaccess 阻擋不是「絕對性阻擋」?(技術解析)

許多人以為 .htaccess 可以阻擋所有壞 BOT,但實際上並非如此,原因如下:

① User-Agent 可以被偽造(壞 BOT 常用技術)

壞 BOT 可以偽裝成:

Mozilla/5.0 (compatible; Googlebot/2.1)

你就無法透過字串判斷它是否為壞 BOT。

② 一些 BOT 甚至不傳 User-Agent(空 UA)

沒有 User-Agent 就無法比對,自然無法阻擋。

③ .htaccess 是 Apache 層阻擋,不是網路層

連線會先到達主機,再由 Apache 判斷是否阻擋。
這代表:

  • 仍然會占用 CPU / RAM
  • 高頻 BOT 仍然會造成網站卡頓

④ 有些 BOT 不走正常 HTTP

例如:

  • 直接 IP 連線掃描
  • 使用非 HTTP 協定
  • 使用 HEAD / OPTIONS 避免觸發判斷

這些都無法被 .htaccess 完全阻擋。

壞 BOT 造成主機負載升高的技術原理

壞 BOT 會造成主機負擔,其原因如下:

① Apache 每個請求都會啟動一條程序

太多 BOT 請求時:

  • Apache process 堆疊
  • CPU 一直跑
  • RAM 被耗盡

② PHP 程式同時被大量觸發

BOT 亂抓 WordPress 頁面=不停觸發 index.php。

③ MySQL 查詢暴增

BOT 抓取文章頁、分類頁 → WordPress 會查詢資料庫。

④ I/O 被占用,磁碟讀取變慢

⑤ 共享主機資源容易被吃光

最常見症狀:

  • 508 Resource Limit Reached
  • 503 Service Unavailable
  • 後台卡住、前台打不開

因此阻擋 BOT 是降低主機負載的重要手段。

在 cPanel 找到並編輯 .htaccess 檔案

或許您可以入 cPanel 控制台>檔案管理員>找 public_html 資料夾下的 .htaccess 檔案
將 .htaccess 檔案 編輯然後加入以下

怎麼找到 .htaccess 檔案,要先記得在檔案管理員中右上角設定裡的開啟隱藏檔案,再次使用搜尋欄中搜尋”.htaccess”,就會找到囉

使用 .htaccess 封鎖常見壞 BOT 清單

引用:

# Begin Bad Bot Blocking
BrowserMatchNoCase OmniExplorer_Bot/6.11.1 bad_bot
BrowserMatchNoCase omniexplorer_bot bad_bot
BrowserMatchNoCase Baiduspider bad_bot
BrowserMatchNoCase Baiduspider/2.0 bad_bot
BrowserMatchNoCase yandex bad_bot
BrowserMatchNoCase yandeximages bad_bot
BrowserMatchNoCase Spinn3r bad_bot
BrowserMatchNoCase sogou bad_bot
BrowserMatchNoCase Sogouwebspider/3.0 bad_bot
BrowserMatchNoCase Sogouwebspider/4.0 bad_bot
BrowserMatchNoCase sosospider+ bad_bot
BrowserMatchNoCase jikespider bad_bot
BrowserMatchNoCase ia_archiver bad_bot
BrowserMatchNoCase PaperLiBot bad_bot
BrowserMatchNoCase ahrefsbot bad_bot
BrowserMatchNoCase ahrefsbot/1.0 bad_bot
BrowserMatchNoCase SiteBot/0.1 bad_bot
BrowserMatchNoCase DNS-Digger/1.0 bad_bot
BrowserMatchNoCase DNS-Digger-Explorer/1.0 bad_bot
BrowserMatchNoCase boardreader bad_bot
BrowserMatchNoCase radian6 bad_bot
BrowserMatchNoCase R6_FeedFetcher bad_bot
BrowserMatchNoCase R6_CommentReader bad_bot
BrowserMatchNoCase ScoutJet bad_bot
BrowserMatchNoCase ezooms bad_bot
BrowserMatchNoCase CC-rget/5.818 bad_bot
BrowserMatchNoCase libwww-perl/5.813 bad_bot
BrowserMatchNoCase magpie-crawler 1.1 bad_bot
BrowserMatchNoCase jakarta bad_bot
BrowserMatchNoCase discobot/1.0 bad_bot
BrowserMatchNoCase MJ12bot bad_bot
BrowserMatchNoCase MJ12bot/v1.2.0 bad_bot
BrowserMatchNoCase MJ12bot/v1.2.5 bad_bot
BrowserMatchNoCase SemrushBot/0.9 bad_bot
BrowserMatchNoCase MLBot bad_bot
BrowserMatchNoCase butterfly bad_bot
BrowserMatchNoCase SeznamBot/3.0 bad_bot
BrowserMatchNoCase HuaweiSymantecSpider bad_bot
BrowserMatchNoCase Exabot/2.0 bad_bot
BrowserMatchNoCase netseer/0.1 bad_bot
BrowserMatchNoCase NetSeer crawler/2.0 bad_bot
BrowserMatchNoCase NetSeer/Nutch-0.9 bad_bot
BrowserMatchNoCase psbot/0.1 bad_bot
BrowserMatchNoCase moreoverbot/5.0 bad_bot
BrowserMatchNoCase Jakarta Commons-HttpClient/3.0 bad_bot
BrowserMatchNoCase SocialSpider-Finder/0.2 bad_bot
Order Deny,Allow
Deny from env=bad_bot
# End Bad Bot Blocking

使用 robots.txt 限制 BOT 抓取範圍

可以把這些清單的 BOT 擋掉然後您可以把以下加到 robots.txt 檔案下,告訴 BOT 不要抓您網站,不過現在不是每個BOT都會遵守 robots.txt 限制便是

user-agent: *
disallow: /cgi
disallow: /images
user-agent: baiduspider
disallow: /
user-agent: yeti
disallow: /
user-agent: omniexplorer_bot
disallow: /
user-agent: yandeximages
disallow: /
user-agent: Spinn3r
disallow: /
user-agent: sogou
disallow: /

常見問題:

Q1.”.htaccess” 可以阻擋所有壞 BOT 嗎?

不行。壞 BOT 可能偽造 User-Agent,甚至不傳送 User-Agent,因此 .htaccess 無法做出絕對阻擋,只能阻擋一部分守規矩或未偽裝的 BOT。

Q2.壞 BOT 為什麼會造成主機負載高?

壞 BOT 不斷對網站發送請求,會觸發 PHP、MySQL 查詢,導致 CPU、RAM、I/O 資源持續被佔用,造成網站變慢甚至無法開啟。

Q3.robots.txt 能阻擋壞 BOT 嗎?

不能。robots.txt 只是「告知」BOT 哪些內容可抓取,是否遵守取決於 BOT 本身,多數惡意 BOT 完全不會理會 robots.txt。

Q4.封鎖 BOT 後需要注意什麼?

建議封鎖前備份 .htaccess,以避免設定錯誤造成網站錯誤。封鎖過程中也要避免誤擋 Google、Bing 等正常搜尋引擎。

文章來源:https://wpoki.com

Written by 婷Z

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

GIPHY App Key not set. Please check settings