asp.net 使用 http2 的 nginx 背后的核心 - 远程 ip 始终为 127.0.0.1
asp.net core behind nginx with http2 - remote ip always 127.0.0.1
我在 Ubuntu 16.04 上有一个 asp.net net core 2.0 应用托管在 Nginx 后面。
我的设置是这样的:
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
时仍然是 return 127.0.0.1
我应该怎么做才能解决这个问题并获得真实的 IP 地址?
好的,自己找答案。
不需要这一行(但是看起来没有做任何有害的事情)
proxy_set_header X-Forwarded-Proto-Version $http2;
但是我们需要告诉 Nginx 设置 X-Forwarded-For
header 用这一行:
proxy_set_header X-Forwarded-For $remote_addr;
我在 Ubuntu 16.04 上有一个 asp.net net core 2.0 应用托管在 Nginx 后面。
我的设置是这样的:
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
时仍然是 return 127.0.0.1
我应该怎么做才能解决这个问题并获得真实的 IP 地址?
好的,自己找答案。
不需要这一行(但是看起来没有做任何有害的事情)
proxy_set_header X-Forwarded-Proto-Version $http2;
但是我们需要告诉 Nginx 设置 X-Forwarded-For
header 用这一行:
proxy_set_header X-Forwarded-For $remote_addr;