IIS 反向代理重新编码包含百分号 (%) 的 URL [英] IIS Reverse Proxy Re-Encoding URLs Containing Percent Sign (%)

查看:56
本文介绍了IIS 反向代理重新编码包含百分号 (%) 的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IIS 7.5 为 Jenkins 设置反向代理,应用程序请求路由 3.0 (ARR) 和 URL 重写 2.0.

I am trying to set up a reverse proxy for Jenkins using IIS 7.5, Application Request Routing 3.0 (ARR), and URL Rewrite 2.0.

我的代理大部分时间都在工作,但遇到包含百分比符号 (%) 的 URL 的问题.

I have the proxy mostly working, but am running into issues with URLs that contain the percent symbol (%).

无论我尝试什么,代理都坚持对重写后的 URL 中的百分号进行解码或重新编码.

No matter what I try, the proxy insists on either de-encoding or re-encoding the percent sign in the rewritten URL.

这就是我想要重写 URL 的方式:

http://my.proxy/a%2Fb -> http://my.host:8080/a%2Fb

URL 是这样实际上被重写的:

This is how the URLs are actually being rewritten:

http://my.proxy/a%2Fb -> http://my.host:8080/a/b
- or -
http://my.proxy/a%2Fb -> http://my.host:8080/a%252Fb

如何让 IIS\ARR\Rewrite 停止重新编码我重写的 URL?

How can I get IIS\ARR\Rewrite to stop re-encoding my rewritten URLs?

我尝试过的事情:

  1. 一个普通的反向代理(将 URL 重写为 http://my.host:8080/a/b):

<match url="(.*)" ignoreCase="true"/><action type="Rewrite" url="http://my.host:8080/{R:1}"/>

使用 UNENCODED_URL 服务器变量(将 URL 重写为 http://my.host:8080/a%252Fb):

Using the UNENCODED_URL server variable (rewrites the URL as http://my.host:8080/a%252Fb):

<match url="(.*)" ignoreCase="false"/><conditions logicalGrouping="MatchAll"><add input="{UNENCODED_URL}" pattern="/(.*)"/></条件><action type="Rewrite" url="http://my.host:8080/{C:1}"/>

直接输入 URL(作为测试 - 也将 URL 重写为 http://my.host:8080/a%252Fb):

Just entering the URL in straight (as a test - also rewrites the URL as http://my.host:8080/a%252Fb):

<match url="(.*)" ignoreCase="false"/><action type="重写" url="http://my.host:8080/a%2Fb"/>

Scott Hanselman 出色的实验中的所有想法:允许百分比,角度-ASP.NET/IIS 请求 URL 中的括号和其他顽皮的东西"

All the ideas in Scott Hanselman's excellent "Experiments in Wackiness: Allowing percents, angle-brackets, and other naughty things in the ASP.NET/IIS Request URL"

  1. <httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="*,:,&amp;,\" RelaxUrlToFileSystemMapping="true"/>
  2. <代码><安全><requestFiltering allowDoubleEscaping="true"/></security>'

注意:当我的 IIS 反向代理与 Jenkins 的内置反向代理检查系统,它尝试将 HTTP 重定向到这种形式的 URL.

Note: I ran into this behavior when my IIS reverse proxy ran afoul of Jenkins' built-in reverse proxy checking system which attempts to do an HTTP redirect to a URL of this form.

推荐答案

Joseph,这是我尝试解决完全相同问题的所有方法的一个很好的总结,让 IIS 使用 SSL 将流量路由到我的 Gerrit 实例.当我发现你的帖子时,我希望也许有人想出了一种神奇的方式来配置它,但我想这是不可能的.我还尝试了另一件事,我已经为 IIS 编写了一个自定义重写提供程序,以便我可以在路由完成之前对百分号进行解码,但是后来我意识到编码发生在以后,这是毫无意义的(我忘记了你的步骤nr 3 表明它非常好).

Joseph, that is a great summary of all the ways I've tried to resolve the exact same issue, having IIS with SSL routing traffic to my Gerrit instance. When I found your post I hoped that maybe someone figured out a magic way to configure it but I guess it's not possible. I have tried one more thing, I've written a custom rewrite provider for IIS so that I can undecode the percent signs before routing is done, but then I realized that the encoding takes place later and this is pointless (I forgot about your step nr 3 that shows it very good).

但是我无法像过去那样摆脱 IIS,所以我想出了一个解决方法.我已经实现了一个简单的服务,它充当 IIS 和 Gerrit 之间的附加代理.当您像在第 2 步中那样配置 IIS 时,转发的请求将使用 %25 代替 url 中的百分比字符.IIS 将请求转发到代理服务,而不是到达 Gerrit.该服务将所有出现的 %25 更改为 %(解码百分比)并将其转发给 Gerrit.不需要对响应做任何事情.对于那些想要走这条路的人,您可以从我在 C# 中简单实现代理开始:

I couldn't however get rid of IIS like out did, so I have figured a workaround. I have implemented a simple service that acts as additional proxy between IIS and Gerrit. When you configure IIS like in step 2, requests that are forwarded will get %25 in place of percent characters in the urls. Instead of reaching Gerrit, IIS forwards the requests to the proxy service. The service changes all occurrences of %25 to % (decodes percents) and forwards it to Gerrit. Nothing needs to be done with the response. For those who want to go this way you can start from my simple implementation of the proxy in C#:

https://gist.github.com/gralin/b5edfd908a41fc7268a77576698af

https://gist.github.com/gralin/b5edfd908a41fc7268a7757698af1e66

这篇关于IIS 反向代理重新编码包含百分号 (%) 的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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