简体   繁体   English

使用nginx时,在dotnet核心中获取真正的客户端IP地址

[英]get real client ip address in dotnet core when using nginx

I'm using dotnet core. 我正在使用dotnet核心。 I'm grabbing the IP address using the line Request.HttpContext.Connection.RemoteIpAddress.ToString() . 我正在使用Request.HttpContext.Connection.RemoteIpAddress.ToString()行获取IP地址。 When I visit my site using my ip address I see the client ip address showing correctly. 当我使用我的IP地址访问我的网站时,我看到客户端IP地址显示正确。 However I want to use https and I use nginx. 但是,我想使用https,我使用nginx。 So in my location I wrote 所以在我写的位置

    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  X-Forwarded-For $remote_addr;
    proxy_set_header  X-Forwarded-Host $remote_addr;

When I visit my site through the domain my ipaddress shows as ::1 and 127.0.0.1 (they switch every time I refresh). 当我通过域访问我的网站时,我的ipaddress显示为::1127.0.0.1 (每次刷新时它们都会切换)。 My nginx config is below I'm not exactly sure how to tell .net core the real ip address 我的nginx配置在下面,我不确定如何告诉.net核心真正的IP地址


server {
    server_name         www.example.com;
    listen              443 ssl;
    ssl_certificate     /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    root                /var/www/example.com/;
    add_header          Strict-Transport-Security "max-age=31536000";
    index index.html index.htm;
    log_not_found off;
    access_log off;
    #try_files $uri.html $uri $uri/ =404;
    expires max;
    default_type text/plain;
    include /etc/nginx/mime.types;

    index off;
    location ~/other/ {
        index off;
        autoindex on;
    }

    location /abc {
        proxy_pass http://localhost:5050;
        proxy_http_version 1.1;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  X-Forwarded-Host $remote_addr;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Forwarded headers are not processed by default. 默认情况下不处理转发的标头。 You need to use HttpOverrides middleware. 您需要使用HttpOverrides中间件。

  • add Microsoft.AspNetCore.HttpOverrides as dependency Microsoft.AspNetCore.HttpOverrides添加为依赖项
  • add the following to your Configure method: 将以下内容添加到Configure方法:

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

I am using .Net Core 2.2, and found the following to work. 我正在使用.Net Core 2.2,并发现以下工作。 It's slightly different than the other answer (which I couldn't get to work). 它与其他答案(我无法开始工作)略有不同。

  • Add Microsoft.AspNetCore.HttpOverrides as dependency 添加Microsoft.AspNetCore.HttpOverrides作为依赖项
  • Add the following to the ConfigureServices method: 将以下内容添加到ConfigureServices方法:

services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders =
        ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
  • Add the following to the Configure method: 将以下内容添加到Configure方法:

app.UseForwardedHeaders();

Source: Configure ASP.NET Core to work with proxy servers and load balancers 源: 配置ASP.NET Core以使用代理服务器和负载平衡器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用 Rails 和 Nginx 获取客户端的真实 IP 地址? - Get the real IP address of client with Rails and Nginx? 在我的 Nginx 入口前使用 ELB 时,如何获得客户端的真实 IP? - How can I get the real IP of a client when using an ELB in front of my Nginx Ingress? 使用 Nginx 反向代理时无法获取客户端的真实 IP - Can't get client's real IP when using Nginx reverse proxy 如何在 Nginx 服务器上获取客户端真正的 ip - How to get client real ip on Nginx server 有什么办法可以在Nginx反向代理上获取真实的客户端IP地址,而无需在服务器端进行任何更改 - Is There Any Ways to Get the Real Client IP Address on Nginx Reverse proxy Without Changing any things on server side 使用nginx lua获取客户端IP地址 - Get client ip address with nginx lua 在一台服务器上使用上游时如何获得真实的客户端 IP? - How to get real client ip when using upstreams on one server? Docker 与 Symfony 和 Nginx 如何获得真实的 IP 地址? - Docker with Symfony an Nginx how to get real ip address? haproxy 背后的 nginx 邮件代理 - 获取客户端的真实 IP 地址 - nginx mail proxy behind haproxy - get clients real ip address 如何使用 Nginx 作为反向代理在 gRPC Java 服务器端获取真实的客户端 IP - How to get real client IP on gRPC Java server side using Nginx as reverse proxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM