Search code examples
nginxasp.net-coreip-addresshttp2

asp.net core behind nginx with http2 - remote ip always 127.0.0.1


I have an asp.net net core 2.0 app hosted behind Nginx on Ubuntu 16.04.

my settings look like that:

server {
        listen 443 ssl http2; 
        listen [::]:443 ssl http2;
        ssl on;
...
    location / {
                    proxy_pass http://localhost:5000;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection keep-alive;
                    proxy_set_header Host $host;
                    proxy_cache_bypass $http_upgrade;
                    proxy_set_header X-Forwarded-Proto-Version $http2;
                    client_max_body_size 32m;
                    keepalive_timeout 200;
                    send_timeout 20;
                    client_body_timeout 50;
            }
}

and I also have these settings in Startup.cs

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

But still every time when I trying to get HttpContext.Connection.RemoteIpAddress it's return 127.0.0.1

What should I do to fix this and get the real IP address?


Solution

  • ok, find answer myself.

    this line unneeded (however seems like don't do anything harmful)

    proxy_set_header X-Forwarded-Proto-Version $http2; 
    

    but we need to say to Nginx to set X-Forwarded-For header with this line:

    proxy_set_header X-Forwarded-For $remote_addr;