nginx反向代理、让代理节点记录客户端真实IP
环境:根据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的更多相关文章
- nginx 代理模式下,获取客户端真实IP
最近做博友推荐,发现个小问题,用$_SERVER['REMOTE_ADDR'];得到的都是服务器的地址192.168.96.52,搜索了一下,发现问题,改为$_SERVER['HTTP_X_REAL_ ...
- Nginx反向代理后应用程序获取客户端真实IP
Nginx反向代理后,Servlet应用通过request.getRemoteAddr()取到的IP是Nginx的IP地址,并非客户端真实IP,通过request.getRequestURL()获取的 ...
- Nginx反向代理后,java获取客户端真实IP地址
一般情况下,java获取客户端IP地址的方法为request.getRemoteAddr();但这只是在没有网关或者代理的情况下,如果客户端将请求发送到nginx,再由nginx进行反向代理到目标服务 ...
- tomcat程序记录客户端真实IP
需求: 开发告知:让后端tomcat日志获取真实的IP,而不是nginx 服务器的IP tomcat前面是nginx做的反向代理,所以tomcat取到的是nginx的ip. 日志名称是localhos ...
- nginx获取经过层层代理后的客户端真实IP(使用正则匹配)
今天帮兄弟项目搞了一个获取客户端真实IP的问题,网上这种问题很多,但是对于我们的场景都不太合用,现把我的解决方案share给大家,如有问题,请及时指出. 场景: 在请求到达后端服务之前,会经过层层代理 ...
- nginx配置解析之客户端真实IP的传递
前后端分离之后,采用nginx作为静态服务器,并通过反向代理的方式实现接口跨域的方式,在降低开发成本的同时也带来了诸多问题,例如客户端真实IP的获取. 在一些特殊场景下,比如风控和支付流程,往往需要获 ...
- 服务器架构前面加了防火墙,Nginx如何获取客户端真实ip???
在大部分实际业务场景中,网站访问请求并不是简单地从用户(访问者)的浏览器直达网站的源站服务器,中间可能经过所部署的CDN.高防IP.WAF等代理服务器.例如,网站可能采用这样的部署架构:用户 > ...
- 【Nginx】如何获取客户端真实IP、域名、协议、端口?看这一篇就够了!
写在前面 Nginx最为最受欢迎的反向代理和负载均衡服务器,被广泛的应用于互联网项目中.这不仅仅是因为Nginx本身比较轻量,更多的是得益于Nginx的高性能特性,以及支持插件化开发,为此,很多开发者 ...
- nginx做负载均衡 tomcat获得客户端真实ip
因项目需要做tomcat2台机器的负载均衡,配置好负载环境后,发现tomcat的日志一律是我前置nginx代理服务器的ip 通过百度教材发现需要修改nginx的配置文件,修改代理头信息,传递给后方,后 ...
随机推荐
- UVA11178 Morley's Theorem(基础模板)
题目链接 题意:给出A,B, C点坐标求D,E,F坐标,其中每个角都被均等分成三份 求出 ABC的角a, 由 BC 逆时针旋转 a/3 得到BD,然后 求出 ACB 的角a2, 然后 由 BC顺时 ...
- C中的字符串实例
1.#include <stdio.h>#include <assert.h>size_t strlen(const char* s){ return ( assert( ...
- listen()
创建一个套接口并监听申请的连接. #include <winsock.h> int PASCAL FAR listen( SOCKET s, int backlog); S:用于标识一个已 ...
- python 内建函数setattr() getattr()
python 内建函数setattr() getattr() setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为v ...
- JS数组类型检测
在强类型语言,数组类型检测是非常容易的事情(typeof就可以解决),而在弱语言JS数据类型就很容易混淆了. JS中常见的数据类型有:number.string.boolean.undefined.f ...
- nodeJS接受post传过来的参数
1.nodeJs接受Post传递的参数需要通过绑定两个事件来获取, querystring = require("querystring"); 1 app.post('/comm ...
- /MD, /MT, /LD (Use Run-Time Library)
https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx
- Effective Objective-C 2.0 — 第9条:以“类族模式”隐藏实现细节
第9条:以“类族模式”隐藏实现细节 类族模式可以把实现细节隐藏在一套简单的公共接口后面 系统框架中经常使用类族 从类族的公共抽象基类中继承子类时要当心,若有开发文档,则应首先阅读 “类族”(class ...
- C-基本语法与运算
编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...
- Yii2 使用八 使用scenarios
在model里定义 public function scenarios() { return [ 'add' => ['title', 'content'], ]; } 在rules里定义 [[ ...