Request.Url.Scheme在负载平衡的站点上提供http而不是https [英] Request.Url.Scheme gives http instead of https on load balanced site

查看:80
本文介绍了Request.Url.Scheme在负载平衡的站点上提供http而不是https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个新的负载平衡过渡站点,并且https是在负载平衡器级别而不是站点级别设置的.另外,此站点将始终为https,因此我不需要远程require https属性等.该URL显示https,但在我的代码中不可用.由于这个原因,我遇到了一些问题

Request.Url.Scheme始终为http:

 公共静态字符串GetProtocol(){var protocol ="http";if(HttpContext.Current!=空&& HttpContext.Current.Request!=空){协议= HttpContext.Current.Request.Url.Scheme;}返回协议;} 

与此基本URL相同的是,协议为http

 公共静态字符串GetBaseUrl(){var baseUrl = String.Empty;如果(HttpContext.Current == null || HttpContext.Current.Request == null || String.IsNullOrWhiteSpace(HttpRuntime.AppDomainAppPath))返回baseUrl;var request = HttpContext.Current.Request;var appUrl = HttpRuntime.AppDomainAppVirtualPath;baseUrl = string.Format("{0}://{1} {2}",request.Url.Scheme,request.Url.Authority,appUrl);如果(!string.IsNullOrWhiteSpace(baseUrl)&&!baseUrl.EndsWith("/"))baseUrl = String.Format("{0}/",baseUrl);返回baseUrl;} 

现在最大的问题是引用样式表中引用的js文件和Google字体.我在这里使用//而不使用http或https,但是这些被视为http,并且在FireBug中看到混合内容阻止消息.

我该如何克服这个问题?

解决方案

正如您所说的那样,HTTPS终止是在负载均衡器级别完成的("https被设置在负载均衡器级别"),这意味着原始方案可能不会到达该站点,具体取决于负载均衡器的配置.

在您的情况下,LB似乎始终配置为通过HTTP与站点对话.因此,您的站点将永远不会在 HttpContext.Request.RawUrl (或类似属性)上看到原始方案.

修复:通常,当以这种方式配置LB,代理或CDN时,会有额外的标头指定原始方案以及可能的其他传入请求参数,例如完整的url,客户端的IP,对于这些代理设备后面的站点将是不直接可见的./p>

I am testing a new load balanced staging site and the https is set up at the load balancer level, not at the site level. Also, this site will be always https so i don't need remote require https attributes etc. The url displays https but it is not available in my code. I have a few issues due to this reason

Request.Url.Scheme is always http:

public static string GetProtocol()
        {
            var protocol = "http";
            if (HttpContext.Current != null && HttpContext.Current.Request != null)
            {
                protocol = HttpContext.Current.Request.Url.Scheme;
            }
            return protocol;
        }

Same thing with this base url, protocol is http

public static string GetBaseUrl()
        {
            var baseUrl = String.Empty;

            if (HttpContext.Current == null || HttpContext.Current.Request == null || String.IsNullOrWhiteSpace(HttpRuntime.AppDomainAppPath)) return baseUrl;

            var request = HttpContext.Current.Request;
            var appUrl = HttpRuntime.AppDomainAppVirtualPath;

            baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);

            if (!string.IsNullOrWhiteSpace(baseUrl) && !baseUrl.EndsWith("/"))
                baseUrl = String.Format("{0}/", baseUrl);

            return baseUrl;
        }

Now the biggest issue is referencing js files and google fonts referenced in the style sheets. I am using // here without http or https but these are treated as http and i see mixed content blocked message in FireBug.

How can i overcome this issue?

解决方案

As you've said HTTPS termination is done at load balancer level ("https is set up at the load balancer level") which means original scheme may not come to the site depending on loadbalancer configuration.

It looks like in your case LB is configured to talk to site over HTTP all the time. So your site will never see original scheme on HttpContext.Request.RawUrl (or similar properties).

Fix: usually when LB, proxy or CDN configured such way there are additional headers that specify original scheme and likely other incoming request parameters like full url, client's IP which will be not directly visible to the site behind such proxying device.

这篇关于Request.Url.Scheme在负载平衡的站点上提供http而不是https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆