码迷,mamicode.com
首页 > 其他好文 > 详细

关于nginx做负载均衡时backend server取real ip

时间:2015-12-08 17:59:45      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

首先:
做为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

更多,查看:

关于nginx做负载均衡时backend server取real ip

标签:

原文地址:http://www.cnblogs.com/ivyer/p/5029402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!