添加 Forwarded 标头Add a Forwarded header
本文介绍 Azure API 管理策略示例,该示例演示如何在入站请求中添加 Forwarded 标头,以允许后端 API 构造正确的 URL。This article shows an Azure API management policy sample that demonstrates how to add a Forwarded header in the inbound request to allow the backend API to construct proper URLs. 若要设置或编辑策略代码,请执行设置或编辑策略中所述的步骤。To set or edit a policy code, follow the steps described in Set or edit a policy. 若要查看其他示例,请参阅策略示例。To see other examples, see policy samples.
代码Code
将代码粘贴到“入站”块中 。Paste the code into the inbound block.
<!-- The policy defined in this file demonstrates how to add a Forwarded header in the inbound request to allow the backend API to construct proper URLs.
<!-- Forwarded header is defined in the IETF RFC 7239 https://tools.ietf.org/html/rfc7239 -->
<!-- Copy this snippet into the inbound section. -->
<policies>
<inbound>
<base />
<set-header exists-action="override" name="Forwarded">
<value>@("proto=" + context.Request.OriginalUrl.Scheme + ";host=" + context.Request.OriginalUrl.Host + ";")</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
后续步骤Next steps
了解有关 APIM 策略的详细信息:Learn more about APIM policies: