使用slb后nginx.apache获取真实ip

前提是在slb设置中开启了“获取真实ip”的功能。

如果只是需要在程序中获取真实ip 那么只要在程序中获取 HTTP_X_FORWARDED_FOR 的值基本就可以了,不需要下面的操作。

下面的操作可以实现nginx或者apache日志中记录用户真实ip或者在apache、nginx中禁止某些ip 用户访问等。

nginx

nginx作为slb获取真实ip是使用 http_realip_module,默认一键安装包安装的nginx没有安装这个模块需要重新重新编译nginx并加装。

以一键安装包为例

重新编译nginx增加 --with-http_realip_module

wget http://soft.phpwind.me/top/nginx-1.0.12.tar.gz

tar zxvf nginx-1.0.12.tar.gz

cd nginx-1.0.12

./configure --user=www --group=www --prefix=/alidata/server/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module

make

make install

kill -USR2 `cat /alidata/server/nginx/logs/nginx.pid`

kill -QUIT `cat /alidata/server/nginx/logs/ nginx.pid.oldbin`

修改nginx.conf

vi /alidata/server/nginx/conf/nginx.conf

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

下面增加

set_real_ip_from ip地址;(这个ip地址首先不是slb提供的公网ip,具体ip多少可以看一下之前nginx日志,如果有多个都要写上。)

set_real_ip_from ip地址;(这个ip地址首先不是slb提供的公网ip,具体ip多少可以看一下之前nginx日志,如果有多个都要写上。)

real_ip_header X-Forwarded-For;

重启nginx

/alidata/server/nginx/sbin/nginx -s reload

apache

安装apache的一个第三方模块"mod_rpaf", 官方网站: http://stderr.net/apache/rpaf/

wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

tar zxvf mod_rpaf-0.6.tar.gz

cd mod_rpaf-0.6

/alidata/server/httpd/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

修改apache配置 /alidata/server/httpd/conf/httpd.conf

在最末尾添加

LoadModule rpaf_module modules/mod_rpaf-2.0.so

RPAFenable On

RPAFsethostname On

RPAFproxy_ips ip地址

RPAFheader X-Forwarded-For

//RPAFproxy_ips ip地址(这个ip地址首先不是slb提供的公网ip,具体ip多少可以看一下apache日志,通常会有2个 都要写上)

添加完成后重启apache

/alidata/server/httpd/bin/apachectl restart

具体案例

LoadModule rpaf_module modules/mod_rpaf-2.0.so

RPAFenable On

RPAFsethostname On

RPAFproxy_ips 10.242.230.65 10.242.230.131

RPAFheader X-Forwarded-For


© 2024 软件教程网 | 联系我们: webmaster# 3062.net.cn