asp.net nginx 背后的核心,带有 http2 - 远程 IP 始终为 127.0.0.1



我在Ubuntu 16.04上托管在Nginx后面的 asp.net net core 2.0应用程序。

我的设置如下所示:

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;
}
}

我在Startup.cs中也有这些设置

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

但每次我试图得到HttpContext.Connection.RemoteIpAddress它仍然会返回127.0.0.1

我应该怎么做才能解决此问题并获取真实的 IP 地址?

好的,自己找到答案。

不需要这一行(但是似乎不要做任何有害的事情(

proxy_set_header X-Forwarded-Proto-Version $http2; 

但是我们需要对 Nginx 说X-Forwarded-For用这一行设置标题:

proxy_set_header X-Forwarded-For $remote_addr;

最新更新