定位 发表于 2017-3-20 21:44:57

nginx proxy获取用户真实ip

  在搭建nginx proxy的时候,要获取用户的真实ip一般的做法是修改网站代码来获取,这里给大家介绍一种不修改网站代码就可以获取到用户真实ip的方法.

  系统:centos 5.9

  需要的软件包:HttpRealIpModule模块

1.编译安装HttpRealIpModule模块
只需要在nginx编译安装中加上--with-http_realip_module这一选项就可以了./configure --user=www --group=www  --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx --with-http_secure_link_module --with-http_random_index_module \
--with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module \
--with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module \
--with-http_perl_module --with-http_geoip_module --with-mail --with-mail_ssl_module2.nginx proxy 配置location / {
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 https://blog.slogra.com;
}3.虚拟主机增加配置
set_real_ip_from nginx_proxy_ip/24;
set_real_ip_from nginx_proxy_ip;
real_ip_header X-Real-IP;

如:
set_real_ip_from 192.168.10.0/24;
set_real_ip_from 192.168.10.6;
real_ip_header X-Real-IP;

4.php获取ip代码
vi i.php
<?php
echo $_SERVER['REMOTE_ADDR'];

然后把i.php放进你网站根目录下,使用浏览器访问https://地址/i.php,就可以看到客户的真实ip地址了.

页: [1]
查看完整版本: nginx proxy获取用户真实ip