架构如下:

lvs 转发请求至nginx  nginx代理域名至源nginx

正常的配置源nginx是取不到用户的真实IP需要增加模块来解决

配置过程如下:

增加一个模块:

http://wiki.nginx.org/NginxChsHttpRealIpModule

需要在编译nginx时增加:

./configure --user=daemon --group=daemon --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_gzip_static_module --with-http_realip_module

然后配置nginx-proxy的配置文件:

server {     listen   80 default;     server_name  _;     index index.php;       location / {              root   /data/www/wwwroot/domain;              proxy_redirect off ;              proxy_set_header Host $host;              proxy_set_header X-Real-IP $remote_addr;              proxy_set_header REMOTE-HOST $remote_addr;              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              client_max_body_size 50m;              client_body_buffer_size 256k;              proxy_connect_timeout 30;              proxy_send_timeout 30;              proxy_read_timeout 60;              proxy_buffer_size 256k;              proxy_buffers 4 256k;              proxy_busy_buffers_size 256k;              proxy_temp_file_write_size 256k;              proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;              proxy_max_temp_file_size 128m;              proxy_pass    http://domain.com;             } }

配置源nginx的配置文件:

   location ~ .*\.php?$     {         fastcgi_pass 127.0.0.1:9000;         fastcgi_index index.php;         fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;         include fastcgi_params;       #set_real_ip_from   192.168.1.0/24;   #允许被信任的段         set_real_ip_from   192.168.1.10;      #允许被信任的IP  加强安全性         real_ip_header     X-Real-IP;     } 

配置好后,日志里就可以看到用户的真实IP了。

在centos 64位上配置生效。测试一切正常。