ocelot在部署时我使用了nginx作为转发,并配置了https证书,但是发现ocelot不支持Forward host header。

https://ocelot.readthedocs.io/en/latest/introduction/notsupported.html

这时候我就有了个疑问,Forward host header到底时什么含义?于是便有了本文。

nginx等代理服务器在转发时,会使用X-Forwarded-For 请求头。该请求头会记录从请求者ip到层层代理服务器ip的信息。

https://imququ.com/post/x-forwarded-for-header-in-http.html

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For

asp.net core 在使用转发服务器后,官方文档说需要使用中间件设置XForwardedFor与XForwardedProto

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2#when-to-use-kestrel-with-a-reverse-proxy

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
}); app.UseAuthentication();

使用X-Forwarded-For会更新 HttpContext.Connection.RemoteIpAddress

使用X-Forwarded-Proto会更新 HttpContext.Request.Scheme

使用X-Forwarded-Host会更新 HttpContext.Request.Host

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.2

我建立了一些测试的webapi来查看效果

        [HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
var remoteIp = HttpContext.Connection.RemoteIpAddress;
string ip = remoteIp.MapToIPv4().ToString();
var scheme = HttpContext.Request.Scheme;
string sch = scheme.ToString();
var host = HttpContext.Request.Host;
string ho = host.ToString();
return new string[] { ip,sch,ho };
}

是否使用中间件,会影响到http和RemoteIPAddress的值是否正确。

而使用了ocelot转发api后,无法读取到host的正确值。ocelot无需添加中间件 ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto

如果添加了,反而会无法获取到正确的remoteip以及scheme

Asp.NET Core Nginx Ocelot ForwardedHeaders X-Forwarded-For的更多相关文章

  1. 部署Asp.net core & Nginx,通过nginx转发

    部署Asp.net core & Nginx,通过nginx转发 CentOS 7 x64 1.vs2017 建立Asp.net core项目,并发布到目录 2.通过FTP工具,将程序copy ...

  2. linux+asp.net core+nginx+sql server

    Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 安装.net core sudo sh -c 'echo "deb ...

  3. linux+asp.net core+nginx四层负载均衡

    Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 关于如何在linux中部署asp.net core我这里不再详细介绍,可以参 ...

  4. [原创]Centos7 安装配置ASP.NET Core+Nginx+Supervisor

    序言 此教程安装的都是最新版本的. 一键安装 有了这个神器,下面的教程就不用做了!只需运行几行代码,直接打开浏览器就可以访问! cd /home/ wget https://files.cnblogs ...

  5. asp.net core nginx配置问题解决

    1.无法访问nginx到发布的站点,但是使用原配置(nginx.conf)却可以.使用新建的配置conf.d/netcore.conf不行. 2.在windows浏览中访问http://xxx.xxx ...

  6. Centos8 Docker+Nginx部署Asp.Net Core Nginx正向代理与反向代理 负载均衡实现无状态更新

    首先了解Nginx 相关介绍(正向代理和反向代理区别) 所谓代理就是一个代表.一个渠道: 此时就涉及到两个角色,一个是被代理角色,一个是目标角色,被代理角色通过这个代理访问目标角色完成一些任务的过程称 ...

  7. 阿里云 asp.net core nginx 单机部署

    1. dotnet core 安装 https://www.microsoft.com/net/download#core 安装之前要安装依赖:yum install libunwind libicu ...

  8. asp.net core网关Ocelot的简单介绍& Ocelot集成Identity认证

    文章简介  Ocelot网关简介 Ocelot集成Idnetity认证处理 Ocelot网关简介 Ocelot是一个基于netcore实现的API网关,本质是一组按特定顺序排列的中间件.Ocelot内 ...

  9. ASP.NET Core下Ocelot的简单使用

    一.创建demo项目 1.新建webapi项目,命名为“DemoProject”,去掉HTTPS勾选 using Microsoft.AspNetCore.Mvc; using System.Coll ...

随机推荐

  1. RabbitMQ Node.js 示例

    RabbitQM 处理和管理消息队列的中间人(broker).可简单理解为邮局,你在程序中写好消息,指定好收件人,剩下的事件就是 RabbitMQ 的工作了,它会保证收件人正确收到邮件. 任何发送邮件 ...

  2. C#数组1

    using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { , , , , , }; ...

  3. Z从壹开始前后端分离【 .NETCore2.1 +Vue 2 +AOP+DI】框架之一 || 前言

    老张 .NetCore与Vue 框架学习目录

  4. LooseVersion()使用及.__version__版本号的获取

    我简单看了distutils库,但发现目前还用不到,感觉有些复杂.因此我简单复制了别人的介绍,如下: Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Python的 ...

  5. Asp.net MVC 中的TempData对象的剖析

    另一篇文章,也对TempData 做了很详细的介绍,链接地址:https://www.jianshu.com/p/eb7a301bc536   . MVC中的 TempData 可以在Controll ...

  6. Unity Pixel 人物设计(1)

    草稿: 目前进度:

  7. vuejs利用props,子组件修改父组件的数据,父组件修改子组件的的数据,数据类型为数组

    博文参考 传送们点一点 父组件: <template> <div> <aa class="abc" v-model="test" ...

  8. [PHP] 安装PHP报错“Primary script unknown”的解决方法

    当安装完最新版的nginx和最新版的PHP以后,使用niginx默认配置文件中的PHP配置会有错误访问指定目录下的php文件时,显示File not found.错误.查看nginx的错误日志显示 9 ...

  9. 用maven运行指定java类main方法

    mvn exec:java -Dexec.mainClass="com.java2s.ide.App"

  10. k8s中的client-go编译成功

    要分版本的,好像1.4跟12差别好大. 1.4中用的模块管理还是vendor,12就换成mod了. 这个要记住差异. 一,从github上下载client-go的1.4版本 https://githu ...