环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html环境配置

  • 在web01或web02上查看用户访问日志

先客户端访问

[root@web_backup /]# for n in {..} ;do curl www.test.com;sleep  ;done
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>

查看日志

[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"

web端记录的都是nginx的IP

修改nignx负载均衡器的/usr/local/nginx/conf/nginx.conf;在location  / 添加  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ; upstream web_pools {
server 192.168.119.130: weight=;
server 192.168.119.133: weight=;
server 192.168.119.131: weight= backup;
} server {
listen ;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

重启nginx服务

[root@lb01 /]# nginx -s stop
[root@lb01 /]# nginx
[root@lb01 /]# netstat -anpt | grep nginx
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx

在web01和web02上修改 /etc/httpd/conf/httpd.conf

[root@web01 /]# vim /etc/httpd/conf/httpd.conf
LogFormat "\"%{x-forwarded-for}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined <VirtualHost *:>
DocumentRoot "/var/www/www"
ServerName www.test.com
ErrorLog "logs/www.test.com.error_log"
CustomLog "logs/www.test.com.access_log" combined
</VirtualHost> <VirtualHost *:>
DocumentRoot "/var/www/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs.test.com.error_log"
CustomLog "logs/bbs.test.com.access_log" combined
</VirtualHost>
[root@web01 /]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@web02 /]# vim /etc/httpd/conf/httpd.conf
LogFormat "\"%{x-forwarded-for}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined <VirtualHost *:>
DocumentRoot "/var/www/www"
ServerName www.test.com
ErrorLog "logs/www.test.com.error_log"
CustomLog "logs/www.test.com.access_log" combined
</VirtualHost> <VirtualHost *:>
DocumentRoot "/var/www/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs.test.com.error_log"
CustomLog "logs/bbs.test.com.access_log" combined
</VirtualHost>

测试:客户端访问

[root@web_backup /]# for n in {..} ;do curl www.test.com;sleep  ;done
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>

到web节点查看日志

[root@web02 /]# tail -f /etc/httpd/logs/www.test.com.access_log
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.b/1.2.3 libidn/1.18 libssh2/1.4.2"
[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

这是apahce的日志信息

如果web节点为nginx服务、则不需要修改、默认已经支持、只需在代理上添加:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;即可

[root@lb02 /]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main;

nginx反向代理、让代理节点记录客户端真实IP的更多相关文章

  1. nginx 代理模式下,获取客户端真实IP

    最近做博友推荐,发现个小问题,用$_SERVER['REMOTE_ADDR'];得到的都是服务器的地址192.168.96.52,搜索了一下,发现问题,改为$_SERVER['HTTP_X_REAL_ ...

  2. Nginx反向代理后应用程序获取客户端真实IP

    Nginx反向代理后,Servlet应用通过request.getRemoteAddr()取到的IP是Nginx的IP地址,并非客户端真实IP,通过request.getRequestURL()获取的 ...

  3. Nginx反向代理后,java获取客户端真实IP地址

    一般情况下,java获取客户端IP地址的方法为request.getRemoteAddr();但这只是在没有网关或者代理的情况下,如果客户端将请求发送到nginx,再由nginx进行反向代理到目标服务 ...

  4. tomcat程序记录客户端真实IP

    需求: 开发告知:让后端tomcat日志获取真实的IP,而不是nginx 服务器的IP tomcat前面是nginx做的反向代理,所以tomcat取到的是nginx的ip. 日志名称是localhos ...

  5. nginx获取经过层层代理后的客户端真实IP(使用正则匹配)

    今天帮兄弟项目搞了一个获取客户端真实IP的问题,网上这种问题很多,但是对于我们的场景都不太合用,现把我的解决方案share给大家,如有问题,请及时指出. 场景: 在请求到达后端服务之前,会经过层层代理 ...

  6. nginx配置解析之客户端真实IP的传递

    前后端分离之后,采用nginx作为静态服务器,并通过反向代理的方式实现接口跨域的方式,在降低开发成本的同时也带来了诸多问题,例如客户端真实IP的获取. 在一些特殊场景下,比如风控和支付流程,往往需要获 ...

  7. 服务器架构前面加了防火墙,Nginx如何获取客户端真实ip???

    在大部分实际业务场景中,网站访问请求并不是简单地从用户(访问者)的浏览器直达网站的源站服务器,中间可能经过所部署的CDN.高防IP.WAF等代理服务器.例如,网站可能采用这样的部署架构:用户 > ...

  8. 【Nginx】如何获取客户端真实IP、域名、协议、端口?看这一篇就够了!

    写在前面 Nginx最为最受欢迎的反向代理和负载均衡服务器,被广泛的应用于互联网项目中.这不仅仅是因为Nginx本身比较轻量,更多的是得益于Nginx的高性能特性,以及支持插件化开发,为此,很多开发者 ...

  9. nginx做负载均衡 tomcat获得客户端真实ip

    因项目需要做tomcat2台机器的负载均衡,配置好负载环境后,发现tomcat的日志一律是我前置nginx代理服务器的ip 通过百度教材发现需要修改nginx的配置文件,修改代理头信息,传递给后方,后 ...

随机推荐

  1. linux 下各文件夹的功能性介绍。(转载)

    原文来自:http://www.cnblogs.com/wen858636827/archive/2012/12/26/2834373.html /opt   放置用户自己下载的软件  英文全称是op ...

  2. webservice理解

    什么是webservice? 1.基于web的一种服务,webservice分为服务器端server和客户端client. server端会会提供一些资源供客户端的应用来访问(获取所需要的数据) 2. ...

  3. Javascript 与正则表达式

    一.正则表达式(regular expression简称res) 二.元字符及其在正则表达式上下文中的行为 三.正则表达式的常用方法 四.与正则表达式有关的字符串对象的方法 五.常用的正则表达式的操作 ...

  4. SVN cleanup操作反复失败解决办法

    今天在更新项目的时候遇到一个问题,按惯例要cleanup才能重新更新.但是很不幸,在cleanup的时候又遇到了问题! 1    svn cleanup failed–previous operati ...

  5. IIS------IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法

    链接: http://www.cnblogs.com/mrma/p/3529859.html

  6. jpa OneToMany

    Project, project_id @OneToMany(mappedBy = "project") private Set<Ap> apSet =new Hash ...

  7. 理解Linux系统/etc/init.d目录和/etc/rc.local脚本

       一.关于/etc/init.d 如果你使用过Linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重 ...

  8. 基于centOS6.7搭建LAMP(httpd-2.4.18+mysql-5.5.47+php-5.6.16)环境

    首先确保系统可以联网.设置IP地址以及虚拟机安装linux在此略过.本文采用centos6.7 64位minimal版.php5.6.16.httpd-2.4.18.mysql-5.5.47版搭建la ...

  9. Using Flash Builder with Flash Professional

    http://help.adobe.com/en_US/flashbuilder/using/WSc5cd04c102ae3e97-6e5d439512e1414e588-8000.html

  10. Django笔记-字符编码相关问题整理

    1.添加中文注释后编译出错,提示:Non-ASCII   解决方法: 在Python脚本文件的第一行或第二行添加一句:      #coding:gbk或#coding:utf-8或##-*- cod ...