Nginx+Docker部署模式下 asp.net core 获取真实的客户端ip
2019-05-06 14:26
375 查看
文章目录
场景
线上环境使用Nginx(安装在宿主机)+Docker进行部署,应用获取客户端ip地址不正确,获取客户端IP的代码为
Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4()
过程还原
搭建一个webapi示例环境
创建一个新项目
dotnet new webapi -o getRealClientIp
修改模板中的
ValuesController的
Get方法
// GET api/values [HttpGet] public ActionResult<string> Get() { return this.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); }
容器相关配置
docker-compose.yml
version: '2' services: web: image: microsoft/dotnet:2.1-aspnetcore-runtime volumes: - ./publish:/app #站点文件 command: dotnet /app/getRealClientIp.dll ports: - "5000:80" networks: test: ipv4_address: 172.200.0.101 nginx: image: nginx networks: test: ipv4_address: 172.200.0.102 volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro # nginx配置文件 ports: - "5001:80" networks: test: ipam: config: - subnet: 172.200.0.0/16 gateway: 172.200.0.1
nginx.conf
http { server { listen 80; access_log off; location / { proxy_pass http://172.200.0.101:80; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Via "nginx"; } } } events { worker_connections 1024; }
运行查看效果
运行
dotnet publish -c Release -o ./publish #编译 docker-compose up #运行容器
直接访问站点
curl http://localhost:5000/api/values 172.200.0.1
返回的ip地址
172.200.0.1是配置的容器的网关地址,能获取到正确的ip
访问通过nginx代理的地址
curl http://localhost:5001/api/values 172.200.0.102
返回的ip地址
172.200.0.102是nginx容器的地址,没有获取到正确的ip
上面的nginx配置已经相关的转发参数,并且该参数配置之前能正常运行在php的环境;
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
推断必须修改 asp.net core 相关代码,才能获取到真实的客户端ip地址,一番google之后,
修改代码如下
// GET api/values [HttpGet] public ActionResult<string> Get() { var ip = this.Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (string.IsNullOrEmpty(ip)) { ip = this.Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); } return ip; }
重新编译运行
dotnet publish -c Release -o ./publish #编译 docker-compose up #运行容器 curl http://localhost:5001/api/values 172.200.0.1 curl http://localhost:5000/api/values 172.200.0.1
直接访问和通过nginx代理访问返回的ip地址均为
172.200.0.1,获取正确。
结论
asp.net core 使用
Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4()获取客户端ip,不会自动取Header中
X-Forwarded-For的值,需求单独处理。
参考资料
相关文章推荐
- asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法
- asp.net 获取客户端真实Ip
- 阿里云容器服务与ASP.NET Core部署:用 docker secrets 保存 appsettings.Production.json
- ASP.NET获取客户端IP及MAC地址
- Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自动化发布和部署
- nginx 代理模式下,获取客户端真实IP
- ASP.NET 获取客户端IP (无视代理)
- ASP.NET获取客户端信息,获取客户端IP等等
- 负载均衡的场景下ASP.NET Core如何获取客户端IP地址
- 在 Docker 中部署 ASP.NET CORE 应用
- 在Heroku上免费部署ASP.NET Core(使用Docker和CircleCI)
- ASP.NET获取客户端IP及MAC地址
- asp.net 获取客户端IP与mac(MSN)
- ASP.NET获取客户端IP及Mac地址
- 基于VS2017的Docker Support体检ASP.NET Core站点的Docker部署
- asp.net 获取客户端IP与mac
- ASP.NET Core 搭配 Nginx 的真实IP问题
- asp.net 获取客户端IP
- ASP.NET CORE做的网站运行在docker上(不用dockerfile文件部署)