您可以設定 Google Cloud Armor 安全性政策,保護服務免受阻斷服務 (DoS) 和網路攻擊。這些政策會使用 Google 的全球基礎架構保護應用程式,並控管流經負載平衡器的流量。
本文提供各種負載平衡器的安全性政策設定範例。
您可以設定 Google Cloud Armor 安全性政策,保護服務免受阻斷服務 (DoS) 和網路攻擊。這些政策會使用 Google 的全球基礎架構保護應用程式,並控管流經負載平衡器的流量。
本文提供各種負載平衡器的安全性政策設定範例。
使用 Cloud de Confiance 控制台或 gcloud CLI 建立安全性政策。這些操作說明假設您要為現有的負載平衡器和後端服務設定政策。如需範例,請參閱「建立範例」。
如要建立規則並將安全性政策附加至後端服務,請按照下列步驟操作:
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下「Create policies」(建立政策)。
在「Name」(名稱) 欄位中,輸入政策名稱。
選用:輸入政策說明。
在「政策類型」中,選擇「後端安全性政策」或「邊緣安全性政策」。
在「預設規則動作」部分,選取「允許」可預設允許存取,選取「拒絕」則預設拒絕存取。
預設規則的優先順序最低,只有在沒有其他適用規則時才會生效。
如果設定「拒絕」規則,請選取拒絕狀態「回應代碼」。 如果要求遭到拒絕,Cloud Armor 會傳回這個狀態碼。
點選「下一步」。
新增更多規則:
選取模式:
在「比對」欄位中,指定規則適用的條件:
在「動作」部分,選取「允許」或「拒絕」。
如要啟用預覽模式,請選取「啟用」。在預覽模式中,Cloud Armor 會記錄規則的效果,但不會強制執行。
輸入規則的「優先順序」 (0 至 2,147,483,646)。請參閱規則評估順序。
按一下「完成」。
如要新增更多規則,請按一下「新增規則」。否則,請點選「下一步」。
對目標套用政策:
如要建立 Cloud Armor 安全性政策,請使用 gcloud compute security-policies create 指令。
使用 --type 旗標指定 CLOUD_ARMOR (後端) 或 CLOUD_ARMOR_EDGE (邊緣)。如果省略這個旗標,Cloud Armor 會建立後端安全性政策:
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 規則語言運算式舉例來說,下列指令會新增規則,封鎖來自 192.0.2.0/24 和 198.51.100.0/24 的流量。規則的優先順序為 1000:
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 的流量,該位址在 User-Agent 標頭中包含字串 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-09-18 (世界標準時間)。