【发布时间】:2021-09-25 03:40:37
【问题描述】:
我的应用有一个不寻常的问题。该应用程序严重依赖于客户端的 IP,有时是 undefined。我的意思是,如果一天中有 10 次访问我的应用,我可以看到我的 7 个客户的 IP,但其中 3 个返回 undefined。
我可以肯定地说,该应用程序没有问题,因为当它托管在具有 CloudFlare 且没有 Nginx 的共享主机中时,它工作得非常好。但是自从我迁移到 Ubuntu 20.04 VPS 并使用 Nginx 和 CloudFlare 后,这个问题就开始了。
我从CloudFlare 开始遵循本教程,希望它能解决问题,但没有。 现在我不知道我应该怎么做才能解决这个问题。所以在这里我希望有人能弄清楚。
这是我在应用端点中获取客户端 IP 的方式:
const clientIp = req.header( 'cf-connecting-ip' ) || req.header( 'true-client-ip' );
nginx.conf 文件如下所示:
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
proxy_hide_header X-Powered-By;
add_header X-Frame-Options SAMEORIGIN;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这是应用配置文件:
server {
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $http_cf_connecting_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
location /blog/ {
access_log /var/log/nginx/blog-example.org_access.log;
error_log /var/log/nginx/blog-example.org_error.log;
root /var/www/html/example.com;
index index.php;
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
#CLOUDFLARE
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
real_ip_header CF-Connecting-IP;
listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
# include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
更新
我不知道这是否是原因,但我收到了GET 对我主页的请求:/xdebug_session_start=phpstorm。我真的不知道那是什么,我怀疑每当它发出请求时,IP 都会返回undefined。
更新
当客户端 ip 为 undefined 时,我已经分析了请求标头。以下是 3 个请求的标头:
headers: {
connection: 'upgrade',
host: '63.250.33.76',
'user-agent': 'Mozilla/5.0 (compatible; CensysInspect/1.1; +https://about.censys.io/)',
accept: '*/*',
'accept-encoding': 'gzip'
}
headers: {
connection: 'upgrade',
host: '63.250.33.76'
}
headers: {
connection: 'upgrade',
host: 'www.example.com',
'user-agent': 'Expanse indexes the network perimeters of our customers. If you have any questions or concerns, please reach out to: scaninfo@expanseinc.com'
}
有人知道这些是什么意思吗?
【问题讨论】:
-
设置
true-client-ip的代理配置在哪里? -
实际上我认为我应该删除它。这是 CF 的付费功能,我没有。除此之外,我已经更新了 OP 关于一个我怀疑可能导致整个问题的新原因。请检查一下。
-
您可以使用
CF-Connecting-IP拉取原始客户端IP,True-Client-IP等效且仅旧设备需要(参见support.cloudflare.com/hc/en-us/articles/…)
标签: node.js express ubuntu nginx cloudflare