加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

web服务器获取用户访问的真实ip地址

(2018-03-27 12:12:06)
标签:

web

获取用户的ip

分类: Linux
    这个话题关联2个方面:
    1.若web服务器之前没有配置反向代理服务器,web服务可以直接获得访问用户IP地址。
    2.若web服务器之前设置了反向代理服务器(nginx或者haproxy),没有修改日志格式的话,web服务的日志获取的访问IP为反向代理服务的IP地址。
    总结一下各种情况:
     一、反向代理服务为nginx,web服务为 Apache
     1.反向代理nginx配置:
        在location 下面添加
         proxy_set_header X-Real-IP $remote_addr;
        Apache配置:
          在主配置文件中,找到如下:
         修改前: 增加了\”%{X-Real-IP}i\”
         LogFormat "%l %u %t \"%r\"%>s %b \"%{Referer}i\" \”%{X-Real-IP}i\”\"%{User-Agent}i\"" combined
        反向代理为nginx,web服务为nginx
      2.反向代理nginx配置:
        在location 下面添加
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       web服务器nginx配置文件中:添加 "$http_x_forwarded_for" 即可
         log_format main '$http_x_real_ip - $remote_user '
                      '[$time_local]  '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';
      二、反向代理为haproxy,web服务器为nginx或Apache。
       反向代理haproxy配置为:
      可以在它的配置文件中加上下面两行:
            option httpclose
             option forwardfor

       web服务器nginx配置文件中:
        添加 "$http_x_forwarded_for" 即可
            log_format main '$http_x_real_ip - $remote_user [$time_local] $status $body_bytes_sent       "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" - $remote_addr';

       web服务器Apache配置文件中:
       添加 \"%{X-ForWarded-For}i\" 即可
       LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\"\"%{X-ForWarded-For}i\" \"%{User-Agent}i\"" combined

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有