why does HttpContext.Request.Url and Request.ServerVariables[“HTTP_HOST”] return different values

StackOverflow https://stackoverflow.com/questions/4419735

문제

I have 3 websites that run from a load-balanced pool of servers. Load-balancer uses sticky sessions to ensure that session stays on initial pool.

In the website, I need to generate a url specific to the website. As per standard procedure I tried to use HttpContext.Request.Url, but this returns the wrong url. I then tried to use Request.ServerVariables["HTTP_HOST"] and that returns the correct url.

Does anyone have any ideas?

On Site A: Request.Url returns Url A Request.ServerVariables["HTTP_HOST"] returns Url A

On Site B:

Request.Url returns Url A Request.ServerVariables["HTTP_HOST"] returns Url B

On Site C:

Request.Url returns Url A Request.ServerVariables["HTTP_HOST"] returns Url C

도움이 되었습니까?

해결책

Request.ServerVariables["HTTP_HOST"] return IIS host, i.e. the host name your website is set to respond to. That may or may not be the same host as in URL client executed to get to your page. The behavior you see is totally logical since client always executes the same URL, but the load balancer send it to different IIS instances.

edit: on that note I am confused as to why would you need to have "a url specific to the website"?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top