Getting client values of IIS Server Variables in Load Balanced Environment

不问归期 提交于 2019-12-30 12:22:02

问题


I have an intranet ASP.NET web application in which I need to get the IP of the client's machine. I do this vis the following code:

HttpContext.Current.Request.ServerVariables.Item("REMOTE_HOST")

It used to work when my ASP.NET site was only hosted on a single server. However once we got the load balancer installed and migrated our apps to a web farm, the code above returns the IP of the Load Balancer device and not of the client anymore.

I am working with the networking folks to determine what can be configured differently with the load balancer, but in the meantime I was wondering if there was another way I could get the client's IP other than using that IIS Server Variable? Or any other suggestions?

Thank you!


回答1:


Which load balancer are you using? It sounds as if your load balancer is acting as a proxy for the web traffic, hence the reason the source appears to come from the LB. Most hardware load balancers are built on Linux platforms and there is provision for transparency if the kernel supports it: http://www.mjmwired.net/kernel/Documentation/networking/tproxy.txt However, this would probably require root access to the unit and some downtime. But it is something that may be worth mentioning to the vendor's support team if they don't have any ideas.

Another (hopefully much easier) option: You may be able to configure the load balancer's proxy to write the client's source IP in the HTTP x-forwarded-for header: http://en.wikipedia.org/wiki/X-Forwarded-For And then you'll be able to read this header via ASP.net in a similar way:

Request.ServerVariables("X-Forwarded-For")

This may already work if the proxy is already doing this.

Really your options depend on what your load balancer is capable of, and what is configurable. Note the list of common hardware vendors at the bottom of the wiki page above.



来源:https://stackoverflow.com/questions/4946538/getting-client-values-of-iis-server-variables-in-load-balanced-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!