首页 > 个人日记 > 用nginx做前端,后端apache能获得真实IP的方法
2011四月25

用nginx做前端,后端apache能获得真实IP的方法

.原文地址:http://isfate.com/511.html
利用nginx做前段缓存的方法在张宴的blog(http://blog.s135.com/nginx_cache/)上已经有说明,不过我们要做些改变,设置为只缓存图片;CSS之类的,避免伪静态被误缓存.现在附上我的nginx配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server
        {
       listen           80;
       server_name      arm.im        www.arm.im;
    location ~ \.(jpg|jpeg|gif|bmp|png|css|js|flv|xml)$
      {
          #  proxy_next_upstream http_502 http_504 error timeout invalid_header;
             proxy_cache cache_one;
             proxy_cache_valid  200 304 12h;
             proxy_cache_valid  302 1m;
             proxy_cache_key $host$uri$is_args$args;
             proxy_set_header Host  $host;
             proxy_set_header X-Forwarded-For  $remote_addr;
             proxy_pass http://50.22.xx.xx;
      }
        location  /
                {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://50.22.xx.xx;
                }
        }

这样就几乎就可以用了,不过还是有些不完美.因为在Apache那端的IP是nginx的IP.所以需要把nginx把客户的真实IP以某种方式发送给apache.
实际上proxy_set_header X-Forwarded-For $remote_addr;这就已经把客户端的真实IP包在http的包头了.只是前端的apache没做处理.这时,需要在前端apache中安装一个模块”rpaf”.rpaf的最新版本在这http://stderr.net/apache/rpaf/ 上面也有安装方法

1
2
3
4
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
tar zxf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

安装完成后会有如下信息:
clip_image001
图中的/usr/lib/apache/mod_rpaf-2.0.so 就是模块位置,要记住,不同的OS安装位置可能不一样.
最后在apache的配置文件的最后中加入

1
2
3
4
5
LoadModule rpaf_module /usr/lib/apache/mod_rpaf-2.0.so(请留意,这里要与安装位置一样,不然找不到模块)
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 173.208.xx.xx  (请写你的nginx前端地址)
RPAFheader X-Forwarded-For

重启httpd.收工

文章作者:suibing
本文地址:http://www.suibing.com/529.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

One Response to “用nginx做前端,后端apache能获得真实IP的方法”

  1. #1 skt 回复 | 引用 Post:2011-04-30 18:45

    都是些专业技术。

    [回复]

发表评论