IIS Reverse Proxy Apply outbound rule on specific path - iis

I have the following inbound and outbound rules defined to get my reverse proxy working.
<rewrite>
<rules>
<rule name="Route the requests for backend app" stopProcessing="true">
<match url="^foldera/folderb/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.site1.com" />
</conditions>
<action type="Rewrite" url="http://www.site2.com/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml" enabled="true" stopProcessing="false">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
<action type="Rewrite" value="/foldera/folderb/{R:1}" />
<conditions>
<add input="{URL}" pattern="^/foldera/folderb/.*" />
</conditions>
</rule>
<preConditions>
<preCondition name="ResponseIsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
Now, the site "http://www.site2.com/" is correclty loaded in "http://www.site1.com/foldera/folderb/" and the outbound rule is making sure that every resource from site2 is rewritten to http://www.site1.com/foldera/folderb/{resourcefromsite1}
Unfortunately, the outbound rule is also crashing the rest of my site. Probably because he's trying to rewrite every native resource to this same "http://www.site1.com/foldera/folderb/" folderstructure.
How can I make the outbound rule only to respond to resources that are requested/loaded through path http://www.site1.com/foldera/folderb/ and for instance not through http://www.site1.com/foldera
Cheers
Jeroen

You were very close to solution. To solve this you must add {URL} as input inside your preCondition. Your rewrite rules should look like this finally:
<rewrite>
<rules>
<rule name="Route the requests for backend app" stopProcessing="true">
<match url="^foldera/folderb/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.site1.com" />
</conditions>
<action type="Rewrite" url="http://www.site2.com/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml" enabled="true" stopProcessing="false">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" />
<action type="Rewrite" value="/foldera/folderb/{R:1}" />
<!-- Removed condition from here -->
</rule>
<preConditions>
<preCondition name="ResponseIsHtml">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{URL}" pattern="^/foldera/folderb/.*" /> <!-- Added your condition here -->
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
This way Outbound Rule will be applied only when Response is HTML and current URL got specified pattern.

Related

IIS can't rewrite outbound rule having soket.io query

I am banging my head into the wall. Every single URL is re-written by IIS URL Rewrite module but the response having https://nginx-server/socket.io/?EIO=3&transport=polling&t=1486150196479-0 when I open my Network tab in chrome, I see:
https://nginx-server.com/socket.io/?EIO=3&transport=polling&t=1486150196479-0
https://nginx-server.com/socket.io/?EIO=3&transport=polling&t=1486150196479-0
https://iis-reverse-proxy-server.com/t/assets/images/chat-logo.png
https://iis-reverse-proxy-server.com/config.js
https://iis-reverse-proxy-server.com/t/assets/images/main_logo.png
I am trying to reverse proxy the https://nginx-server. IIS reverse proxy rewrite all the URL that are accessing nginx except those having socket.io URI in them . Same thing happens when some api is called and the IIS just stop rewriting outbound rules.
this is my web.config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="^(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="{C:1}://nginx-server.com/{R:0}" />
</rule>
</rules>
<outboundRules>
<clear />
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" stopProcessing="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^(.*)?://nginx-server.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}://iis-reverse-proxy-server.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
</configuration>
==========Edit:===========
This is my updated web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
<match url="^(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="HTTP_ACCEPT_ENCODING" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="{C:1}://nginx-server.com/{R:0}" />
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" stopProcessing="true">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^(.*)?://nginx-server.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}://iis-reverse-proxy-server.com/{R:2}" />
</rule>
<rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<rule name="Atag" preCondition="ResponseIsHtml1">
<match pattern="href=(.*?)https://nginx-server.com/(.*?)\s" />
<action type="Rewrite" value="href={R:1}https://iis-reverse-proxy-server.expertflow.com/{R:2}" />
</rule>
<rule name="elementencodedaction" preCondition="ResponseIsHtml1">
<match pattern="action=(.*?)https://nginx-server.com/(.*?)\\" />
<action type="Rewrite" value="‘action={R:1}https://iis-reverse-proxy-server.expertflow.com/{R:2}\" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
<preCondition name="NeedsRestoringAcceptEncoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Where am I making mistake?
Thanks everyone for the efforts for intended help. It turned out the config file in the application re writing the URL so that is why IIS could not be able to rewrite the URL. Updating URL in config file resolved my issue.

How do I get IIS UrlRewrite to handle CSS-delivered woff files appropriately?

Context: Azure; Windows Server 2012; IIS 8
First up, here's the (redacted) web.config for reference
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="false">
<match url="(.*)" />
<action type="Rewrite" url="https://www.khatam.com/{R:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
<rule name="Capture Http Origin Header" enabled="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_ORIGIN}" pattern=".+" />
</conditions>
<serverVariables>
<set name="CAPTURED_ORIGIN" value="{C:0}" />
</serverVariables>
<action type="None" />
</rule>
</rules>
<outboundRules>
<clear />
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="None" pattern="^http(s)?://www.khatam.com/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="http{R:1}://jamuni.pemaish.com.au/{R:2}" />
</rule>
<rule name="Rewrite mundrjatzxera Assets" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="None" pattern="^/(mundrjat/zxera/.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://www.khatam.com/{R:1}" />
</rule>
<rule name="Rewrite Source Srcset in Picture Assets" preCondition="ResponseIsHtml1" enabled="true">
<match filterByTags="CustomTags" customTags="Source Srcset in Picture" pattern=",?\/(mundrjat\/zxera\/\S+\s\d+w)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="https://www.khatam.com/{R:1}" />
</rule>
<rule name="Rewrite X-Frame-Options" enabled="true" patternSyntax="Wildcard">
<match serverVariable="RESPONSE_X-Frame-Options" pattern="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" />
</rule>
<rule name="Set-Access-Control-Allow-Origin for known origins" enabled="true">
<match serverVariable="RESPONSE_Access-Control-Allow-Origin" pattern=".+" negate="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{CAPTURED_ORIGIN}" />
</rule>
<rule name="Restore Accept Encoding" preCondition="Needs to Restore Original Accept Encoding" enabled="true">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="ResponseIsCss">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/css" />
</preCondition>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
<preCondition name="ResponseIsEverything">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)$" />
</preCondition>
<preCondition name="Needs to Restore Original Accept Encoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
</preCondition>
</preConditions>
<customTags>
<tags name="Source Srcset in Picture">
<tag name="source" attribute="srcset" />
</tags>
</customTags>
</outboundRules>
</rewrite>
...
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
We're reverse proxying a site for a client. The proxied site is www.khatam.com. The server through which the proxying is effected is jamuni.pemaish.com.au (yes, I do speak Urdu, albeit not brilliantly.) The client will have an IFRAME in their site which will interact with khatam.com's site via our jamuni server.
So far so good: the client is able to run everything in khatam's site via their IFRAME. What's NOT working so well is that there are WOFF files referenced in the one of the CSS files and these are not loading.
When in devtools in a browser (which is rendering the IFRAME) all the woffs are in red, with the General headers being
Request URL: https://jamuni.pemaish.com.au/vgera.mukljuga/jugabisbis/mukljuga/khatam/vesael/icomoon.woff
Request Method: GET
Status Code: 500 URL Rewrite Module Error.
Remote Address: XXX.XXX.XXX.XXX:443
Referrer Policy: no-referrer-when-downgrade
I have tried to write a rule to change the CSS, viz
<rule name="Rewrite vgera.mukljuga Assets" preCondition="ResponseIsCss" enabled="true">
<match filterByTags="None" pattern="url\((khatam/vesael/.*?.woff)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="url(https://www.khatam.com/vgera.mukljuga/jugabisbis/mukljuga/{R:1}" />
</rule>
but whether the target or the intermediate is specified, I still get the 500 error. Now as I re-read this I'm wondering if I'm having the output of one rewrite being picked up by another leading to a loop or a race. If I take the link from the General above and put it into the address bar of the browser, I get a woff file suggesting that there's contention between two or more rules.
The CSS rules are relative-pathed. Here's one of them:
#font-face {
font-family: trade-gothic-condensed;
src: url(khatam/vesael/tradegothicltcom-bdcn20-webfont.woff) format("woff");
font-weight: 700;
font-style: normal;
-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}
Suggestions welcome.
In the process of getting the Tracing role installed, the 500 error above fixed itself suggesting that the issue had more to do with (likely user-introduced) IIS instability rather than anything else.

Reverse proxy responding with 404 error and response URL incorrect when error

I have a site1 which is a web client application. site1 calls some site2 APIs which is running into CORS issue.
I have written rewrite rules in IIS to rewrite the request matching string extFlow in the URL.
From https://site1/xyz/extFlow/Test.svc/testAPI
to https://site2/extFlow/Test.svc/testAPI
Following is my rewrite rule. The response seems to be written back to https://site1/extFlow/Test.svc/testAPI and not https://site1/xyz/extFlow/Test.svc/testAPI. If site2 responds with 500, the final response from IIS reverse proxy is 404.
<rewrite>
<rules>
<rule name="Route the requests for WFL" stopProcessing="true">
<match url="extFlow/(.*)" />
<conditions>
</conditions>
<action type="Rewrite" url="https://site2/extFlow/{R:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="NeedsResportingAcceptResp" stopProcessing="true">
<match filterByTags="A" serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="NeedsResportingAcceptResp">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<handlers>
<remove name="svc-ISAPI-4.0_64bit" />
<remove name="svc-ISAPI-4.0_32bit" />
<remove name="svc-Integrated-4.0" />
</handlers>
Maybe is an outbound rule missing?
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="None" pattern="^https://site2/extFlow($|/(.*))" />
<action type="Rewrite" value="https://site1/xyz/extFlow/{R:2}" />
</rule>

IIS Rewrite rule, url modify himself

I have write a rewrite rule to redirect an incoming request to the correct server.
Here my web.config :
<rules>
<rule name="ToMonceau">
<match url="test/(.*)" />
<action type="Rewrite" url="http://10.5.5.83/{R:1}" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="AddPrefix" preCondition="IsText" enabled="true">
<match filterByTags="A, Img, Link, Script" pattern="(http://10.5.5.83/mantis/)?(/mantis/)?(.*)" />
<conditions>
<add input="{URL}" pattern="(test/mantis)/(.*)" />
</conditions>
<action type="Rewrite" value="./{R:3}" />
</rule>
<rule name="RestoreAcceptEncofing" preCondition="NeedsRestoringAcceptEncoding">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="IsText">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/(.+)" />
</preCondition>
<preCondition name="NeedsRestoringAcceptEncoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".*" />
</preCondition>
</preConditions>
</outboundRules>
My problem when I go to http://localhost/test/mantis (my server hosts a mantis here), the url change automatically to http://localhost/mantis/, I have to put again the "test/" and it change to http://localhost/mantis/login_page.php. I reput again "test/" and this time the login's page of mantis shown.
The problem continue when I try to log in, the url changes continously by removing the "test/" part.
If I go directly to my mantis without the redirect (http://10.5.5.83/mantis) everything is working like a charm.
What am I missing in my rule to do it correctly ?
I take the exemple here with mantis but I have the same problem on every sites host by different server.
Thanks in advance and sorry if my english is not perfect.
Go to IIS manager and click on URL rewrite:
Keep pattern .*
Action type should be Rewrite
Rewrite URL: http://yourdesiredurl/{R:0}

How to rewrite the request to sub-folder to another application in IIS

if user request to http://my.example.com/pages/uc?dc=qXE7kHEIHHiT2Gexv%2bLDy63yqTeh3gcHsd%2bBrn6vn4%2bArW0gmwv8nw9
then it should be rewrite to http://qa.example.com/uc?dc=qXE7kHEIHHiT2Gexv%2bLDy63yqTeh3gcHsd%2bBrn6vn4%2bArW0gmwv8nw9
The user should not be able to see the URL changes (user should never know its a different application)
<system.webServer>
<rewrite>
<rules>
<rule name="Route the requests for pages" stopProcessing="true">
<match url="^pages/(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url`enter code here`="{C:1}://www.qa.example.com/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://www.qa.example.com/(.*)" />
<action type="Rewrite" value="/pages/{R:2}" />
</rule>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
<action type="Rewrite" value="/pages/{R:1}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
</system.webServer>

Resources