当前位置: 移动技术网 > IT编程>开发语言>.net > nginx反向代理后abp的webapi host如何获取客户端ip?

nginx反向代理后abp的webapi host如何获取客户端ip?

2019年01月25日  | 移动技术网IT编程  | 我要评论

奥尔姆,任怡旭,肤色暗沉吃什么

dotnet core 跨平台是微软伟大的创举,脱离iis后服务器成本都降低了。

问题

这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的ip信息不对。
反向代理后显示的ip信息

解决

这个问题在.net 4.5下处理过,记得当时是继承 webclientinfoprovider重写getclientipaddress。
将代码拿来后发现dotnet core下报错。
跟进后发现 dotnet core下使用的是 abp.aspnetcore.mvc.auditing下的:httpcontextclientinfoprovider

步骤一

修改代码如下,将其放在 xxx.web.core 的extensions目录:

public class webclientinfoproviderfix : iclientinfoprovider
    {
        public string browserinfo => getbrowserinfo();

        public string clientipaddress => getclientipaddress();

        public string computername => getcomputername();

        public ilogger logger { get; set; }

        private readonly ihttpcontextaccessor _httpcontextaccessor;

        private readonly httpcontext _httpcontext;

        /// <summary>
        /// creates a new <see cref="httpcontextclientinfoprovider"/>.
        /// </summary>
        public webclientinfoproviderfix(ihttpcontextaccessor httpcontextaccessor)
        {
            _httpcontextaccessor = httpcontextaccessor;
            _httpcontext = httpcontextaccessor.httpcontext;

            logger = nulllogger.instance;
        }

        protected virtual string getbrowserinfo()
        {
            var httpcontext = _httpcontextaccessor.httpcontext ?? _httpcontext;
            return httpcontext?.request?.headers?["user-agent"];
        }

        protected virtual string getclientipaddress()
        {
            try
            {
                var httpcontext = _httpcontextaccessor.httpcontext ?? _httpcontext;
                
                var headers = httpcontext?.request.headers;
                if (headers!=null&&headers.containskey("x-forwarded-for"))
                {
                    httpcontext.connection.remoteipaddress = ipaddress.parse(headers["x-forwarded-for"].tostring().split(',', stringsplitoptions.removeemptyentries)[0]);
                }
                return httpcontext?.connection?.remoteipaddress?.tostring();
            }
            catch (exception ex)
            {
                logger.warn(ex.tostring());
            }
            return null;
        }

        protected virtual string getcomputername()
        {
            return null; //todo: implement!
        }
    }

步骤二

然后xxxwebcoremodule.cs中添加如下:

            //jieky@2019-1-24 针对 获取客户端ip异常的处理
            configuration.replaceservice(typeof(abp.auditing.iclientinfoprovider), () =>
            {
                iocmanager.register<abp.auditing.iclientinfoprovider, extensions.webclientinfoproviderfix>(abp.dependency.dependencylifestyle.transient);
            });

步骤三

nginx配置例子

server {
    listen 5002;
    access_log  off;
    location / {
       proxy_set_header   x-real-ip        $remote_addr;
       proxy_set_header   host             $host;
       proxy_set_header   x-forwarded-for  $proxy_add_x_forwarded_for;
       proxy_pass                          http://localhost:5000;
    }
}

参考:

asp.net core 搭配 nginx 的真实ip问题

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网