大网数据广告活动
服务器运维问答

NGINX IP Transparency on SSL Stream

· 52CE 我爱测

I am setting up a server cluster with NGINX as the load balancer. All requests are over HTTPS and as such the load balancer uses passes the request straight to the cluster with streams (SSL Pass-through). I need the end servers t…

问题原文

原帖链接 · Jed Hodson · CC BY-SA 3.0

I am setting up a server cluster with NGINX as the load balancer. All requests are over HTTPS and as such the load balancer uses passes the request straight to the cluster with streams (SSL Pass-through).

I need the end servers to see the client IP instead of the load balancer IP.

My nginx.conf

## Misc other config stuff

stream {
  map $ssl_preread_server_name $backend_cluster_map {
    site.a cluster_1;
    site.b cluster_2;
    default cluster_1;
  }

  upstream cluster_1 {
    server 10.0.0.202:443;
    server 10.0.0.203:443;
  }

  upstream cluster_2 {
    server 10.0.0.204:443;
    server 10.0.0.205:443;
  }

  server {
    listen 443;
    proxy_pass $backend_cluster_map;
    ssl_preread on;
  }
}

I tried changing the server block to include proxy_set_header to forward the IP but that didn't pass the nginx test, stating proxy_set_header directive is not allowed here.

server {
  listen 443;
  proxy_protocol on;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Fowarded-Proto https;
  proxy_set_header X-Fowarded-For $remote_addr;
  proxy_set_header X-Fowarded-Host $remote_addr;
  proxy_pass $backend_cluster_map;
  ssl_preread on;
}

I imagine its has something to do with not being able to change the SSL request. Any alternatives? I'm trying not to terminate the SSL at the load balancer.

社区高分解答

原帖链接 · Jed Hodson · CC BY-SA 3.0

Built off what Steffen Ullrich said I followed https://www.nginx.com/resources/admin-guide/proxy-protocol/.

On the end servers I added

server {
  listen 443 ssl proxy_protocol;
  ...
  set_real_ip_from 10.0.0.201;
  real_ip_header proxy_protocol;
  proxy_set_header X-Real-IP $proxy_protocol_addr;
  proxy_set_header X-Fowarded-For $proxy_protocol_addr;
  ...
}

and on the load balancer

server {
  listen 443;
  proxy_protocol on; # Make sure this is enabled
  proxy_pass $backend_cluster_map;
  ...
}

And the application now sees the client IP address not the load balancer's with SSL passthrough.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。