X-Real-IP头在负载平衡器后面用nginx填满

我们有两个networking服务器(nginx)位于负载平衡器后面(风暴点播负载平衡器,而不是由我们的团队pipe理)。 默认情况下,我们收到的IP是负载均衡器IP。 我们尝试在nginx中使用真实的ip模块,但是看起来负载均衡器并没有转发真实的ip。

Nginx的configuration(我们有一个默认的虚拟主机,只是redirect到一个静态的HTML页面进行testing,并inheritance了一切从http指令):

http { # xxxx is the IP of the load balancer set_real_ip_from xxxx; real_ip_header X-Real-IP; # also tried: #real_ip_recursive on; #set_real_ip_from xxx0/24; #set_real_ip_from 0.0.0.0:0; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_real_ip" "$http_x_forwarded_for"'; 

Nginx的版本和编译模块:

 nginx -V nginx version: nginx/1.2.1 built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) TLS SNI support enabled configure arguments: --prefix=/etc/nginx/ --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g' 

日志示例:

 xxxx - - [19/Jul/2012:05:07:17 -0400] "GET /fr/ HTTP/1.1" 200 11110 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.43 Safari/536.11" "-" "-" 

正如您从日志文件中看到的那样,标头HTTP-X-REAL-IP和HTTP-X-FORWARDED-FOR是空的(在日志末尾用“ – ”表示)。 我们尝试了不同的configuration(recursion,0.0.0.0:0),无济于事。

负载平衡器的支持人员说,问题在我们的最后,但是我们不这么认为,因为头是空的。

nginx /真正的IP模块configuration错误会导致头部为空或负载平衡器错误在这里(或第三个选项?)?

仅仅添加就足够了

 server { set_real_ip_from xxxx/x; real_ip_header X-Forwarded-For; 

到您的vhostconfiguration的开头。

看看Storm on Demand负载平衡器服务,看起来他们使用Zeus负载均衡器,它使用X-Cluster-Client-Ip头部作为真实/源IP。 所以我会build议尝试,

 server { set_real_ip_from xxxx/x; real_ip_header X-Cluster-Client-Ip;