From d4dd6c84bcb01f7f46085e5d9bc2b93dbb20119d Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 15 Dec 2017 19:11:48 +0900 Subject: [PATCH] Auto detect NGinx X-Forwarded --- BTCPayServer/Hosting/BTCpayMiddleware.cs | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/BTCPayServer/Hosting/BTCpayMiddleware.cs b/BTCPayServer/Hosting/BTCpayMiddleware.cs index 5ed16443a..913689b7f 100644 --- a/BTCPayServer/Hosting/BTCpayMiddleware.cs +++ b/BTCPayServer/Hosting/BTCpayMiddleware.cs @@ -62,6 +62,40 @@ namespace BTCPayServer.Hosting else httpContext.Request.Host = new HostString(_Options.ExternalUrl.Host, _Options.ExternalUrl.Port); } + // NGINX pass X-Forwarded-Proto and X-Forwarded-Port, so let's use that to have better guess of the real domain + else + { + ushort? p = null; + if(httpContext.Request.Headers.TryGetValue("X-Forwarded-Proto", out StringValues proto)) + { + var scheme = proto.SingleOrDefault(); + if(scheme != null) + { + httpContext.Request.Scheme = scheme; + if (scheme == "http") + p = 80; + if (scheme == "https") + p = 443; + } + } + if (httpContext.Request.Headers.TryGetValue("X-Forwarded-Port", out StringValues port)) + { + var portString = port.SingleOrDefault(); + if(portString != null && ushort.TryParse(portString, out ushort pp)) + { + p = pp; + } + } + if(p.HasValue) + { + bool isDefault = httpContext.Request.Scheme == "http" && p.Value == 80; + isDefault |= httpContext.Request.Scheme == "https" && p.Value == 443; + if (isDefault) + httpContext.Request.Host = new HostString(httpContext.Request.Host.Host); + else + httpContext.Request.Host = new HostString(httpContext.Request.Host.Host, p.Value); + } + } httpContext.Request.Headers.TryGetValue("x-signature", out StringValues values); var sig = values.FirstOrDefault();