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

使用CDKTF创建Azure CDN端点时HostName格式错误的解决咨询

调整Azure CDN端点源HostName格式以解决BadRequest错误

错误信息

Error: creating Endpoint: (Name "test" / Profile Name "test-profile" / Resource Group "test-rg"): cdn.EndpointsClient#Create: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="HostName "https://testmain.z6.web.core.windows.net/" is invalid. It must be a valid domain name, IP version 4, or IP version 6."

相关代码

const cdnProfile = 
new CdnProfile(this, env.CreateId(profileNameId), {
      name: env.CreateId(profileNameId),
      location: 'Global',
      sku: SkuType.Premium_Verizon,
      resourceGroupName: resGroup.name,
});

const hostName = mainStorageAccount.primaryWebEndpoint.replace(/^https?:\/\//, '').replace(/\/$/, '');
const cdnEndpointConfig = {
  name: env.CreateId(cdnEndpointId),
  profileName: cdnProfile.name,
  location: Region.WEST_EUROPE,
  resourceGroupName: resGroup.name,

  querystringCachingBehaviour: 'NotSet',
  optimizationType: 'GeneralWebDelivery',
  isCompressionEnabled: false,
  origin: [
    {
      name: mainStorageAccount.name,
      hostName: hostName,
      httpPort: 80,
      httpsPort: 443,
    },
  ],
};
new CdnEndpoint(this, cdnEndpointConfig.name, cdnEndpointConfig);

解决方案

Azure CDN要求源的HostName必须是纯域名(不带http/https协议前缀、不带末尾斜杠),当前错误显示传入的HostName仍包含https://前缀,说明正则替换逻辑未完全生效。

改用内置的URL解析API处理端点地址,能更可靠地提取纯域名:

// 替换原有的hostName处理代码
const endpointUrl = new URL(mainStorageAccount.primaryWebEndpoint);
const hostName = endpointUrl.hostname;

这种方式无需依赖正则表达式,能自动处理各种合法URL格式,确保提取出的hostName仅包含域名部分(如testmain.z6.web.core.windows.net)。

额外检查项:

  • 确认存储账户的primaryWebEndpoint是有效的静态网站端点
  • 验证处理后的hostName无多余字符(如空格、特殊符号)

内容的提问来源于stack exchange,提问作者stela

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:11:11