Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikael Axel Kleinwort 142 posts 487 karma points c-trib
    Dec 11, 2021 @ 14:58
    Mikael Axel Kleinwort
    0

    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

    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
              <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
                  <match url="localJs/proxiedScriptFile.js" />
                  <action type="Rewrite" url="https://thirdpartyserver.de/js/script.js" />
              </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    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.

    Any idea what I am facing here?

    Kind regards! Mikael

  • Mikael Axel Kleinwort 142 posts 487 karma points c-trib
    Dec 12, 2021 @ 17:16
    Mikael Axel Kleinwort
    1

    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.

  • Diljith Peter 10 posts 140 karma points
    Aug 24, 2022 @ 08:57
    Diljith Peter
    0

    Hi Mikael, Can you elaborate on the steps to solve It?

  • Mikael Axel Kleinwort 142 posts 487 karma points c-trib
    Aug 24, 2022 @ 09:14
    Mikael Axel Kleinwort
    0

    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

  • Diljith Peter 10 posts 140 karma points
    Aug 24, 2022 @ 09:42
    Diljith Peter
    0

    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?

  • Mikael Axel Kleinwort 142 posts 487 karma points c-trib
    Aug 24, 2022 @ 10:01
    Mikael Axel Kleinwort
    1

    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:

    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);
        }
    }
    

    And this is what I have in web.config:

    <configuration>
        ....
        <appSettings>
            ...
        <add key="owin:appStartup" value="DhyMikOwinStartup" />
            ....
        </appSettings>
    </configuration>
    

    Hope this clarifies it!

    Kind regards, Mikael

  • Diljith Peter 10 posts 140 karma points
    Aug 24, 2022 @ 12:59
    Diljith Peter
    0

    Many thanks! This solved my problem.

  • Mikael Axel Kleinwort 142 posts 487 karma points c-trib
    Aug 24, 2022 @ 13:06
    Mikael Axel Kleinwort
    0

    Very cool, I am glad it helped you.

Please Sign in or register to post replies

Write your reply to:

Draft