如何在spring-boot / tomcat(IPv4 / IPv6)中记录来自nginx的x-forwarded-by

时间:2018-06-08 20:09:44

标签: java spring-boot tomcat nginx load-balancing

如何通过CommonsRequestLoggingFilter为tomcat / spring-boot中的nginx记录客户端的真实IP地址,用于IPv4和IPv6?

我有nginx设置来反向代理tomcat(通过spring-boot)并且我在spring-boot server.use-forward-headers: true选项中取得了有限的成功。它适用于IPv4地址;但是,对于IPv6地址,它仍然记录0:0:0:0:0:0:0:1(nginx)。这是一个问题,因为默认情况下,nginx和tomcat都绑定到IPv4和IPv6地址,因此nginx将随机转发给tomcat请求。

示例记录语句:

Before request [uri=/membershiprequest;client=128.250.87.102;user=admin]
Before request [uri=/activity;client=0:0:0:0:0:0:0:1;user=admin]

理想情况下,两者都应该说:

Before request [uri=/membershiprequest;client=128.250.87.102;user=admin]
Before request [uri=/activity;client=128.250.87.102;user=admin]

在这两种情况下,nginx都包含x-forwarded-by标题:

的IPv4

18:43:14.816532 IP 127.0.0.1.56540 > 127.0.0.1.8180:
........GET /activity HTTP/1.0
Host: test.local.org
X-Real-IP: 128.250.87.102
X-Forwarded-For: 128.250.87.102
X-Forwarded-Proto: https

的IPv6

18:43:14.817830 IP6 ::1.37684 > ::1.8180: 
........GET /membershiprequest HTTP/1.0
Host: test.local.org
X-Real-IP: 128.250.87.102
X-Forwarded-For: 128.250.87.102
X-Forwarded-Proto: https

0 个答案:

没有答案