Url Rewrite to another server for specific file (reverse proxy) does not work in Umbraco site
I need to serve a script file from a third-party server as if it is coming from my own server. Should be a simple reverse proxy thing with url rewrite.
Prerequisites:
IIS has Url Rewrite 2.0 and ARR 3.0 installed.
What works:
when I set up an empty localhost website in IIS and add a simple rewrite rule like
I can navigate to localhost/localJs/proxiedScriptFile.js and I get correctly served https://thirdpartyserver.de/js/script.js instead. So ARR and Rewrite is working.
What doesn't work:
When I add the same rewrite rule to the web.config of my existing Umbraco 8 website (on the same IIS), I get a HTTP 404.4 error, as if ARR does not work.
What I tried:
If in Umbraco 8, I change the rewrite to point to a local dummy file of
the same type, it works - the local dummy file is served. But the file off the third-party server is not.
Adding the rewrite path or url to
Umbraco.Core.ReservedUrls or Umbraco.Core.ReservedPaths does not
change the problem.
I solved my problem by using an OWIN middleware as a proxy, rather than using UrlRerwrite/ARR. This worked right away.
If someone comes up with a pure UrlRewrite/ARR solution, I am still interested. Here what works for me:
I found this ReverseProxyMiddleware for .Net Framework. Using this, my OwinStartup contains the following code:
public override void Configuration(IAppBuilder app)
{
// this must come before the base implementation so proxy kicks in before Umbraco
app.UseProxy(
new List<ProxyRule> {
// script proxy
new ProxyRule {
Matcher = uri =>
uri.AbsoluteUri.Contains("localJs/proxiedScriptFile.js")
,
Modifier = (req, user) => {
req.RequestUri = new Uri("https://thirdpartyserver.de/js/script.js");
},
RequiresAuthentication = false
}
},
r =>
{
}
);
base.Configuration(app);
}
Works like a charm. I post it here in case somebody else faces the same challenge.
Thanks for the quick reply. How can I implement this in My Umbraco 8 application which is created using the .net framework? Is it possible to register this in Umbraco Composer?
You create a custom OWIN startup class which inherits from UmbracoDefaultOwinStartup. You then register your custom startup class in web.config. As far as I can remember, that's all which is needed.
My complete custom startup class looks like this:
using Microsoft.AspNetCore.Builder;
using Microsoft.Owin;
using Owin;
using ReverseProxyMiddleware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Hosting;
using Umbraco.Core;
using Umbraco.Web;
[assembly: OwinStartup("DhyMikOwinStartup", typeof(DhyMikOwinStartup))]
namespace Website;
public class DhyMikOwinStartup : UmbracoDefaultOwinStartup
{
public override void Configuration(IAppBuilder app)
{
// this must come before the base implementation so proxy kicks in before Umbraco
app.UseProxy(
new List<ProxyRule> {
// script proxy
new ProxyRule {
Matcher = uri =>
uri.AbsoluteUri.Contains("localJs/proxiedScriptFile.js")
,
Modifier = (req, user) => {
req.RequestUri = new Uri("https://thirdpartyserver.de/js/script.js");
},
RequiresAuthentication = false
}
},
r =>
{
}
);
base.Configuration(app);
}
}
Url Rewrite to another server for specific file (reverse proxy) does not work in Umbraco site
I need to serve a script file from a third-party server as if it is coming from my own server. Should be a simple reverse proxy thing with url rewrite.
Prerequisites: IIS has Url Rewrite 2.0 and ARR 3.0 installed.
What works: when I set up an empty localhost website in IIS and add a simple rewrite rule like
I can navigate to
localhost/localJs/proxiedScriptFile.js
and I get correctly servedhttps://thirdpartyserver.de/js/script.js
instead. So ARR and Rewrite is working.What doesn't work:
When I add the same rewrite rule to the web.config of my existing Umbraco 8 website (on the same IIS), I get a HTTP 404.4 error, as if ARR does not work.
What I tried:
If in Umbraco 8, I change the rewrite to point to a local dummy file of the same type, it works - the local dummy file is served. But the file off the third-party server is not.
Adding the rewrite path or url to Umbraco.Core.ReservedUrls or Umbraco.Core.ReservedPaths does not change the problem.
Any idea what I am facing here?
Kind regards! Mikael
I solved my problem by using an OWIN middleware as a proxy, rather than using UrlRerwrite/ARR. This worked right away.
If someone comes up with a pure UrlRewrite/ARR solution, I am still interested. Here what works for me:
I found this ReverseProxyMiddleware for .Net Framework. Using this, my OwinStartup contains the following code:
Works like a charm. I post it here in case somebody else faces the same challenge.
Hi Mikael, Can you elaborate on the steps to solve It?
Hi Diljjth,
what specifically do you need to know?
I included the above code in the
Configuration(IAppBuilder app)
method of the startup file, more is not needed.Kind regards, Mikael
Hi Mikael,
Thanks for the quick reply. How can I implement this in My Umbraco 8 application which is created using the .net framework? Is it possible to register this in Umbraco Composer?
Hi Diljjith,
no, this does not go into the Composer class.
You create a custom OWIN startup class which inherits from
UmbracoDefaultOwinStartup
. You then register your custom startup class in web.config. As far as I can remember, that's all which is needed.My complete custom startup class looks like this:
And this is what I have in web.config:
Hope this clarifies it!
Kind regards, Mikael
Many thanks! This solved my problem.
Very cool, I am glad it helped you.
is working on a reply...