You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Bicep更新现有应用网关的rewriteRuleSets属性

使用Bicep更新现有Application Gateway的Rewrite Rule Sets

核心注意事项

更新现有应用网关时,绝对不能直接覆盖properties字段,否则会丢失原有配置(如后端池、路由规则、前端IP等)。必须引用现有资源的属性,通过展开语法保留原有设置,再追加或修改目标规则集。

正确实现代码示例

// 引用现有Application Gateway
resource appGateway 'Microsoft.Network/applicationGateways@2024-01-01' existing = {
  name: 'existing-agw'
  scope: resourceGroup('preac_RG')
}

// 更新现有网关,保留原有配置并添加新的Rewrite Rule Set
resource updatedAppGateway 'Microsoft.Network/applicationGateways@2024-01-01' = {
  name: appGateway.name
  location: appGateway.location
  properties: {
    // 展开现有网关的所有属性,避免丢失原有配置
    ...appGateway.properties
    // 合并原有规则集与新规则集(追加模式)
    rewriteRuleSets: [
      // 保留所有已存在的rewriteRuleSets
      ...appGateway.properties.rewriteRuleSets
      // 添加新的CORS规则集
      {
        name: 'cors-test'
        properties: {
          rewriteRules: [
            {
              name: 'test'
              ruleSequence: 5 // 序列值越小,执行优先级越高,避免与现有规则冲突
              conditions: [] // 无规则条件时,使用空数组而非空对象
              actionSet: {
                requestHeaderConfigurations: [
                  {
                    headerName: 'Access-Control-Request-Headers'
                    headerValue: '*'
                  }
                ]
                responseHeaderConfigurations: [
                  {
                    headerName: 'Access-Control-Allow-Origin'
                    headerValue: '*'
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}

进阶场景:替换现有规则集

如果需要替换某个已存在的规则集(而非追加),可以通过过滤语法排除原有规则集,再添加新的:

rewriteRuleSets: [
  // 排除名称为'cors-test'的原有规则集
  ...filter(appGateway.properties.rewriteRuleSets, ruleSet => ruleSet.name != 'cors-test')
  // 添加更新后的规则集
  {
    name: 'cors-test'
    properties: {
      // 新的规则内容
    }
  }
]

关键细节

  • ruleSequence必须唯一,避免与现有规则的序列值冲突,数值越小优先级越高。
  • conditions字段若无规则条件,需设置为空数组[],不能留空对象{},否则会触发部署错误。
  • 使用...展开语法是保留原有配置的核心,确保网关的其他功能不受影响。

内容的提问来源于stack exchange,提问作者Tú Diễm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 02:22:40