负载均衡下如何获取客户端IP

看看 Nginx 配置负载均衡

负载均衡方向代理,可以避免服务器压力。中间服务器选择集群中的其他服务器。

Nginx.conf http 模块

# nginx.conf 中
upstream imacco{
    server 192.168.0.1:10001 weight=3 max_fails=2 fail_timeout =30;
    server 192.168.0.2:10002 weight=3 max_conns=10000;
    server 192.168.0.3:10003 weight=4 ;
    server 192.168.0.4:10004 backup;
}
server { 
    listen 443; 
    ssl on;
    server_name v3.imacco.com;
    ssl_certificate ssl/imacco.com.csr;
    ssl_certificate_key /usr/local/nginx/conf/imacco.com.key;
    ssl_session_timeout 5m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers on;

    location / {
         proxy_pass http://v3.imacco.com; 
         proxy_set_header Host $host; 
         proxy_set_header X-Real-IP $remote_addr; 
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      } 
}


负载均衡理解

upstream imacco{
    # ip_hash指令,将同一用户引入同一服务器。
    ip_hash;
    # 30% 几率,配置允许请求失败的次数=3,fail_timeout=30S 内不会分配给192.168.0.1
    server 192.168.0.1:10001 weight=3 max_fails=2 fail_timeout =30; 
    # 30% 几率 分配最大连接数 max_conns =10000
    server 192.168.0.2:10002 weight=3 max_conns=10000;
    # 40% 几率 
    server 192.168.0.3:10003 weight=4 ;
    # backup 只有其他的都挂了才给192.168.0.4
    server 192.168.0.4:10004 backup;
}

获取 Client-ip

proxy_add_x_forwarded_for 从header 中取

你可能感兴趣的:(linux)