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的配置文件,修改代理头信息,传递给后方,后 ...
随机推荐
- Docker distrubution in django
https://www.syncano.io/blog/configuring-running-django-celery-docker-containers-pt-1/ Update: Fig ha ...
- JavaWeb---总结(十五)JSP基础语法
一.JSP模版元素 JSP页面中的HTML内容称之为JSP模版元素. JSP模版元素定义了网页的基本骨架,即定义了页面的结构和外观. 二.JSP表达式 JSP脚本表达式(expression)用于将 ...
- js008-BOM
js008-BOM 本章内容: 1.理解window对象-BOM的核心 2.控制窗口.框架和弹出窗口 3.利用location对象中的页面信息 4.使用navigation对象了解浏览器 ECMASc ...
- matlab————矩阵输出到txt
matlab如何输出矩阵到txt并指定数的精度 (2013-11-20 16:50:43)转载▼ 标签: matlab 输出数据 假设你的数据矩阵为a; [m n] = size(a); [filen ...
- js随笔,css和js禁止网页选择文本,table的class样式使得td的class样式失效,jquery获得元素坐标
css使用user-select,user-select不是W3C标准,浏览器支持不完整:user-select有两个值,none用户不可以选择文本,text用户可以选择文本 body{-moz-us ...
- Spring MVC学习笔记——用户增删该查和服务器端验证
建立一个动态web项目,起名为SpringMVC_crud 导包,其中包括jstl的一些包等 1.先写一个User.java,是用户类 文件User.java文件 package org.common ...
- CSS 图片加载完成再淡入显示
一.方法 加载完成再显示:借助Image对象的onload事件,加载完时再把src赋给img标签的src: 淡人显示:起始opacity为0,利用transform过度到1 二.代码 <!DOC ...
- os模块之popen
想查看当前目录下有哪些东西,可以使用os.popen()方法,代码如下: t = (os.popen("dir")) print(t.read()) #运行结果 C:\python ...
- 后台程序员的HTTP缓存
1.后端程序员只需要关注请求头: if-None-Match //上一次response头中的ETag的值. 响应头: Etag //是URL的Entity Tag,用于标示URL对象是否改变,区分不 ...
- ecshop新增一个编辑器
在ecshop的后台新增一个编辑器框 步骤一:找到lib_main.php 文件:admin/includes/lib_main.php. 找到变量:function create_html_edit ...