从 nginx 流中获取真实 IP

从 nginx 流中获取真实 IP

我有一个如下的 nginx 配置:

worker_processes 4;
worker_rlimit_nofile 40000;

events {
    worker_connections 8192;
}

stream {
    upstream rancher_servers_http {
        least_conn;
        server <node1>:80 max_fails=3 fail_timeout=5s;
        server <node2>:80 max_fails=3 fail_timeout=5s;
        server <node3>:80 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     80;
        proxy_pass rancher_servers_http;
    }

    upstream rancher_servers_https {
        least_conn;
        server <node1>:443 max_fails=3 fail_timeout=5s;
        server <node2>:443 max_fails=3 fail_timeout=5s;
        server <node3>:443 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     443;
        proxy_pass rancher_servers_https;
    }
}

但我希望在请求中获取客户端真实 IP。我读过https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/并尝试更新 443 服务器如下

server {
        listen     443 proxy_protocol;
        set_real_ip_from <ip of loadabalancer -> where nginx is installed>/24;
        proxy_pass rancher_servers_https;
    }

但是,当我尝试访问指向此负载均衡器的某个站点时,我得到了一个PR_CONNECT_RESET_ERROR

我也尝试添加proxy_protocol on;server部分但随后得到了SSL_ERROR_RX_RECORD_TOO_LONG

我有点迷茫,不知道如何更新我的配置以保持其当前的工作状态并能够检索真实的客户端 IP。

谢谢。

答案1

    set_real_ip_from  192.168.0.0/24;
    real_ip_header    X-Forwarded-For;
    real_ip_recursive on;

相关内容