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

问题

这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的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问题

nginx反向代理后abp的webapi host如何获取客户端ip?的更多相关文章

  1. Nginx反向代理后应用程序获取客户端真实IP

    Nginx反向代理后,Servlet应用通过request.getRemoteAddr()取到的IP是Nginx的IP地址,并非客户端真实IP,通过request.getRequestURL()获取的 ...

  2. nginx反向代理后getRequestURL会出现问题

    nginx反向代理后getRequestURL会出现问题 http://huangqiqing123.iteye.com/blog/1895192

  3. JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)

    1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...

  4. 关于nginx反向代理后获取不到客户端的真实ip地址问题

    前段时间在我的网站上用nginx做了一下反向代理,最近发现不能获取客户端ip了,都是拿到的127.0.0.1的本地ip... 通过查资料后,再去看了看我的配置文件,结果发现我没有如下配置: nginx ...

  5. nginx反向代理后应用程序如何获取客户端真实IP

    每个location中增加配置: proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_se ...

  6. Nginx反向代理后,java获取客户端真实IP地址

    一般情况下,java获取客户端IP地址的方法为request.getRemoteAddr();但这只是在没有网关或者代理的情况下,如果客户端将请求发送到nginx,再由nginx进行反向代理到目标服务 ...

  7. Nginx反向代理+DNS轮询+IIS7.5 千万PV 百万IP 双线 网站架构案例

    原文地址:http://www.jb51.net/article/31844.htm Nginx  ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 ...

  8. 关于配置Nginx反向代理后SpringSecurity认证失败的问题解决

    问题背景 最近在写的一个项目,采用前后端分离的方式进行开发,登录认证使用的是SpringSecurity框架. 问题描述 在项目部署的时候出现了一个问题,在自己电脑上运行的时候一切顺畅,可是部署到服务 ...

  9. nginx 反向代理报400错误与Host关系

    转载自:https://blog.csdn.net/qq_22208737/article/details/80787396 如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段 ...

随机推荐

  1. 读Understanding the Linux Kernel, 3rd Edition有感

    14.3.2.2. Avoiding request queue congestion Each request queue has a maximum number of allowed pendi ...

  2. 如何在Solr中实现多core查询

    基于solr或者elasticsearch提供的多核,多索引,多shard等查询能力,一般都是由lucene提供的多索引查询的功能演化而来的,这个功能在单机版的lucene里面确实没有发挥多大的威力, ...

  3. Solr -- 查询语法/参数

    1. 常用查询参数 参数 描述 defType 指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucene sort 指定响应的排序方式:升序asc或降序desc.同时需要指 ...

  4. thymeleaf 之 th:each迭代循环对象集合

    thymeleaf 之 th:each迭代循环对象集合 2018年02月24日 14:32:31 阅读数:1382 th:each属性用于迭代循环,语法:th:each="obj,iterS ...

  5. mybatis相对于ibatis的优势

    2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...

  6. 回到顶部最简单的JQuery实现代码

    CSS代码,使用了fixed让对象固定于浏览器窗口: top{position:fixed;bottom:0;right:10px;} jQuery代码,注意正常使用的几个条件:$('#top').c ...

  7. LUA全总结

    ------------------------------------------------------------------------------ --2018.7.21 do --开启或关 ...

  8. C#【Thread】Interlocked 轻量级锁

    什么说它是轻量级呢?因为它仅对整形数据(即int类型,long也行)进行同步. 具体使用如下表: Interlocked.Increment(ref value) 数值加一(原子性操作) Interl ...

  9. md5,原理待续

    以前项目中copy出来的 import java.security.MessageDigest; public class MD5Util { /** * @todo MD5加码 生成32位md5码 ...

  10. webkit com wrapper 推荐!

    https://groups.google.com/forum/#!topic/microsoft.public.vb.general.discussion/ZaFY95aDZoY http://ww ...