URL Rewrite keeps original host Location when reverse proxy 301 redirects

﹥>﹥吖頭↗ 提交于 2019-11-27 04:30:28

问题


I have URL Rewrite setup on an IIS 7.5 site: http://site1.com/

This acts as a reverse proxy to the second site: http://site2.com/

Here is the flow of events:
1. Browser does a GET on http://site1.com/somepath
2. This gets passed through to site2 because site1 is the URL Rewrite reverse proxy. This works well and the host is correctly set because I've done the mod that requires this.
3. site2 responds with a 301 status and sets the HTTP Location header to http://site3.com/somenewpath
4. site1 responds to the browser with a 301 but replaces the host in the Location header with site1: http://site1.com/somenewpath

What I want to happen in step 4 is that site1 responds with http://site3.com/somenewpath in the HTTP Location header and does a straight pass through of this data. I feel that there must be an Outbound rule that can be applied to solve this but haven't been able to figure it out yet.


回答1:


Could Application Request Routing be involved? Look at IIS -> Machine or Site -> Application Request Routing Cache -> Server Proxy Settings and uncheck the "Reverse rewrite host in response headers" checkbox. If you do this at the machine level, it'll take effect for all sites. If you do it on a particular site, it'll only take effect for that site, and other sites on the box will be unaffected.




回答2:


As I said in the above comments, I believe the default behavior of the reverse proxy is to pass through the response untouched (assumes there are no outbound rewrite rules set). I haven't tested your scenario specifically with a 301 response from the server behind the proxy, though.

If a special outbound rule is in fact needed, this code will modify the HTTP location header of all 301 responses to http://site3.com/somepath

<outboundRules>
  <!-- This rule changes the domain in the HTTP location header for redirect responses -->
  <rule name="Change Location Header">
    <match serverVariable="RESPONSE_LOCATION" pattern="^http://[^/]+/(.*)" />
    <conditions>
      <add input="{RESPONSE_STATUS}" pattern="^301" />
    </conditions>
    <action type="Rewrite" value="http://www.site3.com/{R:1}" />
  </rule>
</outboundRules>

This rule is a slight modification of one posted in URL Rewrite Module 2.0 Configuration Reference



来源:https://stackoverflow.com/questions/23508938/url-rewrite-keeps-original-host-location-when-reverse-proxy-301-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!