当前位置: > Linux服务器 > nginx >

nginx做负载均衡时backend server如何取得真实IP

时间:2014-06-19 02:30来源:linux.it.net.cn 作者:IT网

首先,作为loadblance的nginx需要添加下面的配置:
 

复制代码 代码如下:proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
X-Real-IP和X-Forwarded-For可以2选1,也可以都打开。

然后:
1.如果backend是apache时:apache需要添加模块rpaf
http://stderr.net/apache/rpaf/
可以用于apache 2.2

Compile and Install for 1.3:
apxs -i -a -c mod_rpaf.c

Compile and Install for 2.0:
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

or simply try:
make

Configuration Directives:
RPAFenable On
# Enable reverse proxy add forward
RPAFproxy_ips 127.0.0.1 10.0.0.1
# which ips are forwarding requests to us
RPAFsethostname On
# let rpaf update vhost settings
# allows to have the same hostnames as in the "real"
# configuration for the forwarding Apache
RPAFheader X-Forwarded-For
# Allows you to change which header mod_rpaf looks
# for when trying to find the ip the that is forwarding
# our requests

# if DSO load module first: LoadModule rpaf_module libexec/apache2/mod_rpaf-2.0.so RPAFenable On RPAFsethostname On RPAFproxy_ips 192.168.1.23 (nginx loadblance server) RPAFheader X-Forwarded-For

2.如果是nginx做为backend的话:
作为backend的nginx在编译时需要添加模块
--with-http_realip_module

并修改nginx.conf:
 

复制代码 代码如下:set_real_ip_from   192.168.2.1;(该ip为nginx loadblance ip)
real_ip_header     X-Real-IP;
context为: http, server, location

更多内容,请参考:
http://wiki.nginx.org/NginxHttpRealIpModule


 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容