選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

搭配使用 CreateRule 與 CLI - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

搭配使用 CreateRule 與 CLI

下列程式碼範例示範如何使用 CreateRule

CLI
AWS CLI

範例 1:使用路徑條件和轉送動作建立規則

如果 URL 包含指定的模式,以下create-rule範例會建立規則,將請求轉送到指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 5 \ --conditions file://conditions-pattern.json --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

conditions-pattern.json 的內容:

[ { "Field": "path-pattern", "PathPatternConfig": { "Values": ["/images/*"] } } ]

範例 2:使用主機條件和固定回應建立規則

如果主機標頭中的主機名稱符合指定的主機名稱,則以下create-rule範例會建立提供固定回應的規則。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 10 \ --conditions file://conditions-host.json \ --actions file://actions-fixed-response.json

conditions-host.json 的內容

[ { "Field": "host-header", "HostHeaderConfig": { "Values": ["*.example.com"] } } ]

actions-fixed-response.json 的內容

[ { "Type": "fixed-response", "FixedResponseConfig": { "MessageBody": "Hello world", "StatusCode": "200", "ContentType": "text/plain" } } ]

範例 3:使用來源 IP 地址條件、驗證動作和轉送動作建立規則

下列create-rule範例會建立規則,在來源 IP 地址符合指定的 IP 地址時驗證使用者,並在身分驗證成功時將請求轉送至指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 20 \ --conditions file://conditions-source-ip.json \ --actions file://actions-authenticate.json

conditions-source-ip.json 的內容

[ { "Field": "source-ip", "SourceIpConfig": { "Values": ["192.0.2.0/24", "198.51.100.10/32"] } } ]

actions-authenticate.json 的內容

[ { "Type": "authenticate-oidc", "AuthenticateOidcConfig": { "Issuer": "https://idp-issuer.com", "AuthorizationEndpoint": "https://authorization-endpoint.com", "TokenEndpoint": "https://token-endpoint.com", "UserInfoEndpoint": "https://user-info-endpoint.com", "ClientId": "abcdefghijklmnopqrstuvwxyz123456789", "ClientSecret": "123456789012345678901234567890", "SessionCookieName": "my-cookie", "SessionTimeout": 3600, "Scope": "email", "AuthenticationRequestExtraParams": { "display": "page", "prompt": "login" }, "OnUnauthenticatedRequest": "deny" }, "Order": 1 }, { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:880185128111:targetgroup/cli-test/642a97ecb0e0f26b", "Order": 2 } ]
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateRule

PowerShell
Tools for PowerShell

範例 1:此範例會根據指定接聽程式的客戶標頭值,使用固定回應動作建立新的接聽程式規則。

$newRuleAction = [Amazon.ElasticLoadBalancingV2.Model.Action]@{ "FixedResponseConfig" = @{ "ContentType" = "text/plain" "MessageBody" = "Hello World" "StatusCode" = "200" } "Type" = [Amazon.ElasticLoadBalancingV2.ActionTypeEnum]::FixedResponse } $newRuleCondition = [Amazon.ElasticLoadBalancingV2.Model.RuleCondition]@{ "httpHeaderConfig" = @{ "HttpHeaderName" = "customHeader" "Values" = "header2","header1" } "Field" = "http-header" } New-ELB2Rule -ListenerArn 'arn:aws:elasticloadbalancing:us-east-1:123456789012:listener/app/testALB/3e2f03b558e19676/1c84f02aec143e80' -Action $newRuleAction -Condition $newRuleCondition -Priority 10

輸出:

Actions : {Amazon.ElasticLoadBalancingV2.Model.Action} Conditions : {Amazon.ElasticLoadBalancingV2.Model.RuleCondition} IsDefault : False Priority : 10 RuleArn : arn:aws:elasticloadbalancing:us-east-1:123456789012:listener-rule/app/testALB/3e2f03b558e19676/1c84f02aec143e80/f4f51dfaa033a8cc
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateRule

AWS CLI

範例 1:使用路徑條件和轉送動作建立規則

如果 URL 包含指定的模式,以下create-rule範例會建立規則,將請求轉送到指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 5 \ --conditions file://conditions-pattern.json --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

conditions-pattern.json 的內容:

[ { "Field": "path-pattern", "PathPatternConfig": { "Values": ["/images/*"] } } ]

範例 2:使用主機條件和固定回應建立規則

如果主機標頭中的主機名稱符合指定的主機名稱,則以下create-rule範例會建立提供固定回應的規則。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 10 \ --conditions file://conditions-host.json \ --actions file://actions-fixed-response.json

conditions-host.json 的內容

[ { "Field": "host-header", "HostHeaderConfig": { "Values": ["*.example.com"] } } ]

actions-fixed-response.json 的內容

[ { "Type": "fixed-response", "FixedResponseConfig": { "MessageBody": "Hello world", "StatusCode": "200", "ContentType": "text/plain" } } ]

範例 3:使用來源 IP 地址條件、驗證動作和轉送動作建立規則

下列create-rule範例會建立規則,在來源 IP 地址符合指定的 IP 地址時驗證使用者,並在身分驗證成功時將請求轉送至指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 20 \ --conditions file://conditions-source-ip.json \ --actions file://actions-authenticate.json

conditions-source-ip.json 的內容

[ { "Field": "source-ip", "SourceIpConfig": { "Values": ["192.0.2.0/24", "198.51.100.10/32"] } } ]

actions-authenticate.json 的內容

[ { "Type": "authenticate-oidc", "AuthenticateOidcConfig": { "Issuer": "https://idp-issuer.com", "AuthorizationEndpoint": "https://authorization-endpoint.com", "TokenEndpoint": "https://token-endpoint.com", "UserInfoEndpoint": "https://user-info-endpoint.com", "ClientId": "abcdefghijklmnopqrstuvwxyz123456789", "ClientSecret": "123456789012345678901234567890", "SessionCookieName": "my-cookie", "SessionTimeout": 3600, "Scope": "email", "AuthenticationRequestExtraParams": { "display": "page", "prompt": "login" }, "OnUnauthenticatedRequest": "deny" }, "Order": 1 }, { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:880185128111:targetgroup/cli-test/642a97ecb0e0f26b", "Order": 2 } ]
  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateRule

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。