系统相关
首页 > 系统相关> > 带有http2的Nginx背后的asp.net核心-远程IP始终为127.0.0.1

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

作者:互联网

我在Ubuntu 16.04的Nginx后面托管了一个asp.net网络核心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;

标签:asp-net-core,nginx,ip-address,http2
来源: https://codeday.me/bug/20191109/2011235.html