如何实现 Nginx 代理的节点访问日志记录客户的 IP 而不是代理的 IP?

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介:

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

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

先客户端访问

[root@web_backup /]# for n in {1..20} ;do curl www.test.com;sleep 1 ;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 - - [29/Sep/2016:22:14:33 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:35 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:37 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:39 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:41 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:51 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:53 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:55 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:57 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:00 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:02 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:04 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:06 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:08 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:10 +0800] "GET / HTTP/1.0" 200 22

 

web端记录的都是nginx的IP

 

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

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream web_pools {
    server 192.168.119.130:80 weight=5;
    server 192.168.119.133:80 weight=5;
    server 192.168.119.131:80 weight=5   backup;
}

    server {
        listen       80;
        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:80                  0.0.0.0:*                   LISTEN      2113/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 *:80>
    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 *:80>
    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 *:80>
    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 *:80>
    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 {1..10} ;do curl www.test.com;sleep 1 ;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 - - [29/Sep/2016:22:36:56 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:36:58 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:01 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:03 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:05 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:41 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:43 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:45 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:47 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:49 +0800] "GET / HTTP/1.0" 200 22"192.168.119.131" - - [29/Sep/2016:22:41:23 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:41:25 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:41:27 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:41:29 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:41:31 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:16 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:18 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:20 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:22 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:24 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:26 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:28 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:30 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:32 +0800] "GET / HTTP/1.0" 200 22 "-" "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" - - [29/Sep/2016:22:33:34 +0800] "GET / HTTP/1.0" 200 22 "-" "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;
'















本文转自浅景尘51CTO博客,原文链接:http://blog.51cto.com/857803451/1948034 ,如需转载请自行联系原作者


相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
29天前
|
存储 监控 算法
防止员工泄密软件中文件访问日志管理的 Go 语言 B + 树算法
B+树凭借高效范围查询与稳定插入删除性能,为防止员工泄密软件提供高响应、可追溯的日志管理方案,显著提升海量文件操作日志的存储与检索效率。
73 2
|
6月前
|
存储 应用服务中间件 nginx
在使用Nginx之后,如何在web应用中获取用户IP以及相关原理
但总的来说,通过理解网络通信的基础知识,了解http协议以及nginx的工作方式,我们已经能在大多数情况下准确地获取用户的真实IP地址了,在调试问题或者记录日志时会起到很大的帮助。
329 37
|
10月前
|
网络协议 应用服务中间件 网络安全
Nginx,正向代理
本文介绍了Nginx作为HTTPS正向代理的两种方案:HTTP CONNECT隧道(7层)和NGINX stream(4层)。HTTP CONNECT隧道需要客户端手动配置代理,通过CONNECT请求建立隧道;而NGINX stream则更适合透明代理,利用SNI字段实现流量转发。文章详细讲解了两者的原理、环境搭建、使用场景及常见问题,并提供了配置示例和最佳实践建议。内容转载自阿里云开发者社区@怀知的文章,推荐读者参阅原文获取更多信息。感谢您的阅读!
1376 80
Nginx,正向代理
|
8月前
|
域名解析 应用服务中间件 网络安全
阿里云个人博客外网访问中断应急指南:从安全组到日志的七步排查法
1. 检查安全组配置:确认阿里云安全组已开放HTTP/HTTPS端口,添加规则允许目标端口(如80/443),授权对象设为`0.0.0.0/0`。 2. 本地防火墙设置:确保服务器防火墙未阻止外部流量,Windows启用入站规则,Linux检查iptables或临时关闭防火墙测试。 3. 验证Web服务状态:检查Apache/Nginx/IIS是否运行并监听所有IP,使用命令行工具确认监听状态。 4. 测试网络连通性:使用外部工具和内网工具测试服务器端口是否开放,排除本地可访问但外网不可的问题。 5. 排查DNS解析:确认域名A记录指向正确公网IP,使用`ping/nslookup`验证解析正
297 2
|
12月前
|
安全 应用服务中间件 网络安全
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
661 60
|
12月前
|
安全 应用服务中间件 网络安全
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
683 60
|
11月前
|
缓存 Java 应用服务中间件
nginx的正向代理和反向代理以及tomcat
Nginx的正向代理和反向代理功能在不同的场景中具有重要作用,正向代理主要用于客户端访问控制和匿名浏览,而反向代理则用于负载均衡和高可用性服务。Tomcat作为Java Web应用服务器,与Nginx结合使用,可以显著提升Web应用的性能和稳定性。通过合理配置Nginx和Tomcat,可以构建高效、稳定和可扩展的Web服务架构。
426 11
|
11月前
|
应用服务中间件 Linux 网络安全
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
3531 8
|
11月前
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
979 3
|
负载均衡 前端开发 JavaScript
Nginx 代理多服务
以上是 Nginx 代理多服务的几种常见方式,在实际应用中,可以根据具体的业务需求和系统架构选择合适的代理方式,并结合其他 Nginx 的功能和配置来优化和完善系统的性能和功能。

热门文章

最新文章