我正在使用 dotnet 核心。我正在使用线路获取 IP 地址Request.HttpContext.Connection.RemoteIpAddress.ToString()
。当我使用我的 IP 地址访问我的网站时,我看到客户端 IP 地址显示正确。不过我想使用 https 并且我使用 nginx。所以在我的位置我写了
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;
当我通过域访问我的网站时,我的 IP 地址显示为::1
and 127.0.0.1
(每次我刷新时它们都会切换)。我的 nginx 配置如下,我不太确定如何告诉 .net core 真实的 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;
}
}
默认情况下不处理转发的标头。您需要使用 HttpOverrides 中间件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)