Umbraco with reverse proxy or Dev Tunnel
# help-with-umbraco
m
I'm working on a site that needs to get webhook requests from external providers, for this the new "Dev Tunnels"-feature in Visual Studio is great. The problem is that Umbraco does not recognize the x-forwarded-host header which makes any absolute links point to https://localhost/ since that is what it's running as internally. I have a slight memory that there might be some "mechanism" in Umbraco that tells the CMS the current URL (so that I can use the headers from the proxy). Anyone knows?
k
There's the
UmbracoApplicationUrl
setting, but I'm not sure what it controls...
a
@Markus Johansson you can add something like this to your rewrite rules:
Copy code
csharp
private static void RewriteHttpHost(RewriteContext context) {

    context.Result = RuleResult.ContinueRules;

    // Get a reference to the current request
    HttpRequest request = context.HttpContext.Request;

    // Get the host from the header
    string? proxyHost = request.Headers["x-forwarded-host"];
    if (!string.IsNullOrWhiteSpace(proxyHost)) return;

    // Update the HTTP host
    request.Host = new HostString(proxyHost);

}
m
Thanks @Anders Bjerner I have to give that a try! Is that wired up in during service registration? I noticed that the built-in UrlProvider is using HttpContext.Request directly - I'm guessing that this would basically fool the site to think that it's operating on a different host right?
a
Yes, Umbraco and other logic should see the "new" host instead
m
Great stuff! Thanks!
a
> Is that wired up in during service registration? For an Umbraco 10 site, I have this in my
Configure
method in `Startup.cs`:
Copy code
csharp
app.UseRewriter(new RewriteOptions().Add(RewriteHttpHost));
The Umbraco 13 template doesn't have a
Startup.cs
, but it looks somewhat similar in
Program.cs
. I can see my coworker added it between
await app.BootUmbracoAsync();
and
app.UseUmbraco()
. I don't know if it makes any difference, but probably wouldn't hurt adding it before
await app.BootUmbracoAsync();
. https://cdn.discordapp.com/attachments/1222868066858438737/1222930014157406258/image.png?ex=66180154&is=66058c54&hm=9b2200ac47d37ff80605a97a46b54512ad889b89c9e52473a09f12b73f797a38&
m
Thanks man! Relly helpful! It works great! I ended up with this:
Copy code
csharp
public class ProxyHostRewriteRule : Microsoft.AspNetCore.Rewrite.IRule
{
    public void ApplyRule(RewriteContext context)
    {
        context.Result = RuleResult.ContinueRules;

        // Get a reference to the current request
        HttpRequest request = context.HttpContext.Request;

        // Get the host from the header
        string? proxyHost = request.Headers["x-forwarded-host"];
        if (!string.IsNullOrWhiteSpace(proxyHost))
        {
            // Update the host
            request.Host = new HostString(proxyHost);
        }

        string? proxyScheme = request.Headers["x-forwarded-scheme"];
        if (!string.IsNullOrWhiteSpace(proxyScheme))
        {
            // Update the scheme (http/https)
            request.Scheme = proxyScheme;
        }

    }
}
and then during startup:
Copy code
csharp
var rewriteOptions = new RewriteOptions();
rewriteOptions.Rules.Add(new ProxyHostRewriteRule());
app.UseRewriter(rewriteOptions);
Thanks a lot for helping out!
20 Views