【问题标题】:How to preserve client ip while using apache2 as proxy to tomcat8如何在使用 apache2 作为 tomcat8 的代理时保留客户端 IP
【发布时间】:2018-03-30 23:43:52
【问题描述】:

我有一个 Apache2 服务器,除了在 Tomcat8 服务器上的 localhost:8080 托管一些其他将 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 作为代理服务器时一样。

提前致谢。

【问题讨论】:

    标签: spring-boot apache2 ubuntu-16.04 tomcat8


    【解决方案1】:

    问题的解决需要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 标头。

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

    或简单地添加:

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

    我希望这对其他人有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-05
      • 2017-02-08
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多