首页 技术 正文
技术 2022年11月10日
0 收藏 691 点赞 2,273 浏览 1832 个字

asp.net core程序部署在centos7(下面的解决方案,其他系统都能使用,这里只是我自己部署在centos7),使用服务器jexus进行部署,AppHost模式。

因为请求是由jexus进行了转发的,所以asp.net zero获取的ip永远都是127.0.0.1.。

解决方案:

使用由Jexus作者宇内流云提供的JwsIntegration替换IISIntegration,它改变默认从请求头获取ip的规则,改为由 “X-Original-For”获取远程ip(经测试 使用”X-Real-IP”也能获取)。

JwsIntegration.cs:

    /// <summary>
/// 用于处理客户IP地址、端口的HostBuilder中间件
/// </summary>
public static class WebHostBuilderJexusExtensions
{ /// <summary>
/// 启用JexusIntegration中间件
/// </summary>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IWebHostBuilder UseJexusIntegration(this IWebHostBuilder hostBuilder)
{
if (hostBuilder == null)
{
throw new ArgumentNullException(nameof(hostBuilder));
} // 检查是否已经加载过了
if (hostBuilder.GetSetting(nameof(UseJexusIntegration)) != null)
{
return hostBuilder;
} // 设置已加载标记,防止重复加载
hostBuilder.UseSetting(nameof(UseJexusIntegration), true.ToString()); // 添加configure处理
hostBuilder.ConfigureServices(services =>
{
services.AddSingleton<IStartupFilter>(new JwsSetupFilter());
}); return hostBuilder;
} } class JwsSetupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseMiddleware<JexusMiddleware>();
next(app);
};
}
} class JexusMiddleware
{
readonly RequestDelegate _next;
public JexusMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions<IISOptions> options)
{
_next = next;
} public async Task Invoke(HttpContext httpContext)
{
var headers = httpContext.Request.Headers; try
{
if (headers != null && headers.ContainsKey("X-Original-For"))
{
var ipaddAdndPort = headers["X-Original-For"].ToArray()[0];
var dot = ipaddAdndPort.IndexOf(":", StringComparison.Ordinal);
var ip = ipaddAdndPort;
var port = 0;
if (dot > 0)
{
ip = ipaddAdndPort.Substring(0, dot);
port = int.Parse(ipaddAdndPort.Substring(dot + 1));
} httpContext.Connection.RemoteIpAddress = System.Net.IPAddress.Parse(ip);
if (port != 0) httpContext.Connection.RemotePort = port;
}
}
finally
{
await _next(httpContext);
} } }

使用方法:

  asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,943
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,098
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766