.net 在nginx下获取到的客户端ip是127.0.0.1

钟景华 发布于 2016/04/29 12:22
阅读 1K+
收藏 0

获取到的ip是127.0.0.1


在配置文件中加入了:

server {
        listen       80;
        server_name  ws.5iyaya.com;

        charset utf-8;

        location / {
            proxy_pass   http://ws.5iyaya.com;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
C#中获取ip
string _ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            System.Web.HttpRequest requset = System.Web.HttpContext.Current.Request;
            

            // If there is no proxy, get the standard remote address
            if (_ip == null || _ip.ToLower() == "unknown")
                _ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

System.Web.HttpContext.Current.Request.ServerVariables["X-Forwarded-For"]


不是127,就是空的


也重启过nginx了

加载中
0
tinshen
tinshen
X-Real-IP 也要取值判断。
钟景华
X-Real-IP 也取出来试过了
0
seriawei
seriawei

我是这样配置的

location / {
    proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

没发现会取不到IP。

返回顶部
顶部