基于自定义 HTTP 标头的 UrlRewrite 条件 [英] UrlRewrite condition based on custom HTTP header

查看:21
本文介绍了基于自定义 HTTP 标头的 UrlRewrite 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UrlRewrite 中配置具有 2 个条件的规则:

I'm trying to configure a rule in UrlRewrite that has 2 conditions:

  • HTTP 标头 HTTP_HOST 需要匹配某个域(api.contoso.com)
  • 自定义 HTTP 标头 x-app-version 需要出现在 HTTP 请求中
  • HTTP header HTTP_HOST needs to match a certain domain (api.contoso.com)
  • A custom HTTP header x-app-version needs to be present in the HTTP request

根据此信息,我想重定向到后端的不同版本的 API.

based on this info, I'd like to redirect to a different version of the API in the back-end.

问题该规则在第一个条件下按预期运行.当 HTTP_HOST 匹配 api.contoso.com 时,它就会启动.但是,它忽略了我的自定义 x-app-version 标头.

Problem The rule is behaving as expected on the first condition. It kicks in when HTTP_HOST matches api.contoso.com. However, it ignores my custom x-app-version header.

假设因此,我担心 UrlRewrite 条件只能与下拉列表中有限的预定义 HTTP 标头集(HTTP_HOSTREMOTE_ADDRESSREMOTE_HOST, …)

Assumption So, I fear that a UrlRewrite condition only can be defined in combination with the limited set of predefined HTTP headers from the dropdown (HTTP_HOST, REMOTE_ADDRESS, REMOTE_HOST, …)

问题假设是正确的还是应该有可能?我的配置或其他方法中是否存在基于自定义 HTTP 标头的条件的错误?

Question Is assumption correct or should this be possible whatsoever? Is there a mistake in my config or other approach to have conditions based on a custom defined HTTP header?

<rule name="ARR_test2" enabled="false">
  <match url="(.*)" />  
  <conditions>
    <add input="{HTTP_HOST}" pattern="api.contoso.com" />
    <add input="{x-app-version}" pattern="1" />
  </conditions>
  <action type="Rewrite" url="https://FARM_apiv1/{R:0}" />
</rule>

推荐答案

好的,我发现了如何在 UrlRewrite 条件中使用自定义 HTTP 标头:

Ok, I found out how to use custom HTTP headers in a UrlRewrite condition:

  • 自定义标头需要以HTTP_"开头.
  • 用下划线代替破折号

例如:为了检索我的自定义标头x-app-version",我可以使用HTTP_x_app_version".所以我的 UrlRewrite 配置应该是这样的

E.g.: in order to retrieve my custom header "x-app-version", I can use "HTTP_x_app_version". So my UrlRewrite config should look like this

<rule name="ARR_test2" enabled="false">
  <match url="(.*)" />  
  <conditions>
    <add input="{HTTP_HOST}" pattern="api.contoso.com" />
    <add input="{HTTP_x_app_version}" pattern="1" />
  </conditions>
  <action type="Rewrite" url="https://FARM_apiv1/{R:0}" />
</rule>

实际上在文档中提到的很清楚:https://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

It's actually mentioned quite clear in the documentation: https://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

这篇关于基于自定义 HTTP 标头的 UrlRewrite 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆