spring-boot - 如何在使用 apache2 作为 tomcat8 的代理时保留客户端 ip

标签 spring-boot apache2 ubuntu-16.04 tomcat8

我有一个 Apache2 服务器,它除了在本地主机:8080 的 Tomcat8 服务器上托管一些将 https 转换为 http 的其他内容之外,它正在运行一个 spring-boot 应用程序。为此,我们在可用站点中使用以下配置

    ProxyPass / http://0.0.0.0:8080/
    ProxyPassReverse / http://0.0.0.0:8080/

这一切都在为我们工作。直到我们观察到由于我们错过的一个明显原因而无法记录客户端 IP。由于我们的请求现在由 apache2 代理,我们收到 127.0.0.1 作为我们的客户端 ip。现在我们如何确保我们接收到有效的客户端 ip,就像之前我们没有使用 apache2 作为代理服务器时的情况一样。

提前致谢。

最佳答案

该问题的解决方案需要Proxy在转发前在请求的X-Forwarded-For Header中保存主机信息。在 Apache2 中,这是通过添加

ProxyPreserveHost On

以及相应 VirtualHost 配置的 conf 中的其他信息。可以通过在日志中添加 X-Forwarded-For header 来验证此实现。在 Apache2 中,您必须为 LogFormat 编辑 /etc/apache2/apache2.conf,如下所述:

How can I configure my Apache server to log my client's public IP addresses

/var/log/apache2/access.log 处观察 X-Forwarded-For header 的日志。

一旦您确信这一点,您现在可以配置 Tomcat8 以在 /etc/tomcat8/server.xml 中配置一个阀门,如 here 所述:

或简单地添加:

   <Valve className="org.apache.catalina.valves.RemoteIpValve"  internalProxies="127.0.0.1" remoteIpHeader="x-forwarded-for" />

希望对其他人有帮助

关于spring-boot - 如何在使用 apache2 作为 tomcat8 的代理时保留客户端 ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576782/

相关文章:

java - spring boot 依赖注入(inject)

python:子进程在使用 sudo 时给我不同的结果

Angular Service Worker SwUpdate.available 未触发

Python 包安装在错误的文件夹中

wordpress - 如何使用 Ubuntu 16.04 克服 Wordpress 中的错误 "Unable to connect to the filesystem"并尝试使用 FTPS SSL

Spring Consul - 禁用单元测试

azure - 如何使用 Spring Boot 应用程序通过 Azure Functions 安排 Cosmos DB 数据处理

java - 在 spring boot 中加载 war 文件之外的属性文件

https - 如何重定向https ://to https://www

apache2 - Apache2 中不允许使用符号链接(symbolic link)