您可以設定 Google Cloud Armor 安全性政策,保護服務免受阻斷服務 (DoS) 和網路攻擊。這些政策會使用 Google 的全球基礎架構保護應用程式,並控管流經負載平衡器的流量。
本文提供各種負載平衡器的安全性政策設定範例。
您可以設定 Google Cloud Armor 安全性政策,保護服務免受阻斷服務 (DoS) 和網路攻擊。這些政策會使用 Google 的全球基礎架構保護應用程式,並控管流經負載平衡器的流量。
本文提供各種負載平衡器的安全性政策設定範例。
您可以使用 Cloud de Confiance 控制台或 gcloud CLI 建立安全性政策。本節的說明假設您要設定安全性政策,並套用至現有的全域外部應用程式負載平衡器或傳統應用程式負載平衡器和後端服務。如需如何填寫欄位的範例,請參閱「建立範例」。
建立 Cloud Armor 安全性政策和規則,並將安全性政策附加至後端服務:
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下「Create policies」(建立政策)。
在「Name」(名稱) 欄位中,輸入政策名稱。
選用:輸入政策說明。
在「政策類型」中,選擇「後端安全性政策」或「邊緣安全性政策」。
在「預設規則動作」部分,選取「允許」,表示預設規則允許存取;選取「拒絕」,表示預設規則禁止存取 IP 位址或 IP 位址範圍。
預設規則的優先順序最低,只有在沒有其他適用規則時才會生效。
如果您要設定「拒絕」規則,請選取「拒絕狀態」訊息。 如果沒有存取權的使用者嘗試取得存取權,Cloud Armor 就會顯示這則錯誤訊息。
無論要設定哪種規則,請點選「下一步」。
新增更多規則:
選取模式:
在「比對」欄位中,指定規則的適用條件:
在「動作」中選取「允許」或「拒絕」,允許或拒絕符合規則的流量。
如要啟用預覽模式,請選取「啟用」核取方塊。在預覽模式中,您可以查看規則的運作方式,但規則不會啟用。
輸入規則的「Priority」(優先順序)。可以是介於 0 到 2,147,483,646 之間的任何正整數 (含首尾)。如要進一步瞭解評估順序,請參閱「規則評估順序」。
按一下 [完成]。
如要新增更多規則,請按一下「新增規則」,然後重複上述步驟。 否則,請點選「下一步」。
對目標套用政策:
如要建立新的 Cloud Armor 安全性政策,請使用 gcloud compute security-policies create 指令。
在 type 欄位中,使用 CLOUD_ARMOR 建立後端安全政策,或使用 CLOUD_ARMOR_EDGE 建立邊緣安全政策。type 旗標為選用項目;如未指定類型,系統會預設建立後端安全性政策:
gcloud compute security-policies create NAME \
[--type=CLOUD_ARMOR|CLOUD_ARMOR_EDGE] \
[--file-format=FILE_FORMAT | --description=DESCRIPTION] \
[--file-name=FILE_NAME]
更改下列內容:
NAME:安全性政策的名稱FILE_FORMAT:--file-name 中指定的檔案格式;請指定 yaml 或 jsonDESCRIPTION:安全政策說明FILE_NAME:包含安全政策 YAML 或 JSON 匯出內容的檔案名稱下列指令會更新您先前建立的政策、開啟 JSON 剖析功能,並將記錄層級變更為 VERBOSE:
gcloud compute security-policies update my-policy \
--json-parsing=STANDARD \
--log-level=VERBOSE
如要將規則新增至安全性政策,請使用 gcloud
compute security-policies rules create PRIORITY 指令。
gcloud compute security-policies rules create PRIORITY \
[--security-policy POLICY_NAME] \
[--description DESCRIPTION] \
--src-ip-ranges IP_RANGE,... | --expression EXPRESSION \
--action=[ allow | deny-403 | deny-404 | deny-502 ] \
[--preview]
更改下列內容:
PRIORITY:要在政策中指派給規則的優先順序。如要瞭解規則優先順序的運作方式,請參閱「規則評估順序」一文。POLICY_NAME:安全性政策名稱DESCRIPTION:規則說明IP_RANGE,...:以半形逗號分隔的 IP 位址範圍清單EXPRESSION:Cloud Armor 規則語言運算式舉例來說,下列指令會新增規則,封鎖來自 IP 位址範圍 192.0.2.0/24 和 198.51.100.0/24 的流量。這項規則的優先順序為 1000,且是名為「my-policy」政策中的規則。
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \
--src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \
--action "deny-403"
新增 --preview 旗標後,系統會將規則新增至政策,但不會強制執行,且只會記錄觸發規則的流量。
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \
--src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \
--action "deny-403" \
--preview
使用 --expression 旗標指定自訂條件。詳情請參閱「設定自訂規則語言屬性」。下列指令會新增規則,允許來自 IP 位址 1.2.3.4 的流量,且使用者代理程式標頭中包含字串 example:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')" \
--action allow \
--description "Block User-Agent 'example'"
下列指令會新增規則,如果要求的 Cookie 含有特定值,就會封鎖要求:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')" \
--action "deny-403" \
--description "Cookie Block"
下列指令會新增規則,封鎖來自 AU 地區的要求:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "origin.region_code == 'AU'" \
--action "deny-403" \
--description "AU block"
下列指令會新增規則,封鎖來自 AU 區域的要求,但指定 IP 範圍內的要求除外:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "origin.region_code == 'AU' && !inIpRange(origin.ip, '1.2.3.0/24')" \
--action "deny-403" \
--description "country and IP block"
下列指令會新增規則,封鎖 URI 符合規則運算式的要求:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "request.path.matches('/example_path/')" \
--action "deny-403" \
--description "regex block"
下列指令會新增規則,如果 user-id 標頭的 Base64 解碼值包含特定值,就會封鎖要求:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')" \
--action "deny-403" \
--description "country and IP block"
下列指令會新增規則,使用預先設定的運算式來防範 SQLi 攻擊:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "evaluatePreconfiguredWaf('sqli-v422-stable')" \
--action "deny-403"
下列指令會新增規則,使用預先設定的運算式,允許具名 IP 位址清單中的所有 IP 位址存取:
gcloud compute security-policies rules create 1000 \
--security-policy my-policy \
--expression "evaluatePreconfiguredWaf('sourceiplist-fastly')" \
--action "allow"
本節說明如何為區域性外部應用程式負載平衡器,設定區域範圍的 Cloud Armor 安全性政策。
請按照下列步驟設定安全性政策,保護區域範圍的後端服務:
建立區域範圍安全性政策。
gcloud compute security-policies create POLICY_NAME \
--type=CLOUD_ARMOR \
--region=REGION
更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:要建立安全性政策的區域將區域範圍的安全性政策附加至區域範圍的後端服務。將 BACKEND_NAME 替換為現有區域範圍後端服務的名稱。
gcloud compute backend-services update BACKEND_NAME \
--security-policy=POLICY_NAME \
--region=REGION
舉例來說,假設您是安全管理員,希望滿足所有後端工作負載和網路應用程式防火牆 (WAF) 規則都部署在特定區域的駐留需求。假設您已事先完成下列事項:
您可以使用下列範例指令,在政策中新增 WAF 規則和其他進階規則,同時滿足需求:
為政策新增 WAF 規則:
gcloud compute security-policies rules create 1000 --action=deny-404 \
--expression="evaluatePreconfiguredWaf('xss-v422-stable', ['owasp-crs-v042200-id941100-xss', 'owasp-crs-v042200-id941160-xss'])" \
--security-policy=POLICY_NAME \
--region=REGION
更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:安全政策的區域為政策新增進階規則:
gcloud compute security-policies rules create 1000 --action=allow \
--expression="has(request.headers['cookie']) && request.headers['cookie'].contains('80=EXAMPLE')" \
--security-policy=POLICY_NAME \
--region=REGION
為政策新增頻率限制規則:
gcloud compute security-policies rules create 1000 --action=throttle \
--src-ip-ranges="1.1.1.1/32" \
--rate-limit-threshold-count=1000 \
--rate-limit-threshold-interval-sec=120 \
--conform-action="allow" \
--exceed-action="deny-429" \
--enforce-on-key=IP \
--ban-duration-sec=999 \
--ban-threshold-count=5000 \
--ban-threshold-interval-sec=60 \
--security-policy=POLICY_NAME \
--region=REGION
更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:安全政策的區域除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-07-13 (世界標準時間)。