【问题标题】:Client IP is sometimes undefined in a Nodejs app hosted in Ubuntu with CloudFlare and Nginx客户端 IP 有时在使用 CloudFlare 和 Nginx 托管在 Ubuntu 中的 Nodejs 应用程序中未定义
【发布时间】: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


【解决方案1】:

这些客户端是否有可能直接访问您的应用程序而不是通过 Cloudflare?例如,如果您的服务器未配置为接受来自 Cloudflare IP ranges 的流量,那么有人可能会直接访问您的应用,这可以解释为什么在某些情况下未设置 CF-Connecting-IP

更多信息请访问documentation page

【讨论】:

  • 是的,这可能是一个原因。我已经在我的服务器中允许了 CF 地址。有没有其他方法可以阻止客户端直接连接到我的服务器?
  • 通常您会将防火墙配置为仅接受来自 Cloudflare IP 范围的 HTTPS 流量,并拒绝其他任何试图从其他任何地方访问它的流量。这里有一个很好的ufw教程digitalocean.com/community/tutorials/…
  • 你是对的。这些请求直接发送到我的服务器而不是 CF。我在本教程的帮助下修复了它。 developers.cloudflare.com/ssl/origin-configuration/…
  • 很高兴听到这个消息。 Authenticated Origin Pulls 绝对是解决这个问题的好方法。
【解决方案2】:

我对 cloudflare 不太熟悉,但你能在你的代码中添加“x-real-ip”标头并检查一下吗?

const clientIp = req.header( 'cf-connecting-ip' ) || req.header( 'true-client-ip' ) || req.header( 'x-real-ip' );

【讨论】:

  • 嘿。我之前尝试过,但有时我仍然不确定。您是否阅读了 OP 中的更新?
  • 你能试试这个并检查它是否适合我- const clientIp = req.headers['cf-connecting-ip' ] || req.headers(['true-client-ip' ] || req.headers['x-real-ip'];
  • 我独自一人拥有X real Ip。我有时会获得客户端 IP,但有时也会获得未定义的 IP。
猜你喜欢
  • 2021-01-19
  • 2018-06-29
  • 1970-01-01
  • 2019-05-17
  • 2015-06-29
  • 2020-07-21
  • 1970-01-01
  • 2020-04-25
  • 2017-05-16
相关资源
最近更新 更多