spring-boot - Apache 无法连接到新的 tomcat 9 ajp

标签 spring-boot apache tomcat

将 Spring boot 的版本从 2.1.4 升级到 2.3.2 后,我的 apache2 无法再(通过 ajp)连接到我的 Spring boot 的嵌入式 tomcat。
它显示以下错误:

[proxy:error] [pid xxxx ] (111)Connection refused: AH00957: AJP: attempt to connect to 10.0.75.1:8500 (10.0.75.1) failed
[proxy_ajp:error] [pid xxxx ] [client xxx ] AH00896: failed to make connection to backend: 10.0.75.1, referer: http://myapp.develop/home/
我的开发环境是这样设置的:
  • 一个 Angular 应用程序(运行在 4200 上的节点服务器)
  • Spring Boot 后端(在 8500 端口上的 tomcat 上设置的 ajp 连接器)
  • 前端 apache2 服务器(在 docker 容器上)设置为将请求重定向到两个应用程序:
    <VirtualHost *:80>
    ServerName myapp.develop
    
    ProxyPass "/home" "http://10.0.75.1:4200/home"
    ProxyPassReverse "/home" "http://10.0.75.1:4200/home"
    
    ProxyPass "/backend" "ajp://10.0.75.1:8500/backend"
    ProxyPassReverse "/backend" "ajp://10.0.75.1:8500/backend"
    

  • 我通过/etc/hosts 上的域名访问我的网络应用程序:myapp.develop
    这是我的spring boot tomcat的配置
       Connector connector = new Connector("AJP/1.3");
       connector.setScheme("http");
       connector.setPort(8500);
       connector.setSecure(false);
       connector.setAllowTrace(false);
       ((AbstractAjpProtocol) connector.getProtocolHandler()).setSecretRequired(false);
    
    在 app.properties 中:
    tomcat.ajp.port=8500
    tomcat.ajp.remoteauthentication=false
    tomcat.ajp.enabled=true
    
    这是 tomcat 日志:
    o.s.b.w.e.t.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http) 8500 (http)
    o.a.c.h.Http11NioProtocol    : Initializing ProtocolHandler ["http-nio-8080"]
    o.a.c.a.AjpNioProtocol       : Initializing ProtocolHandler ["ajp-nio-127.0.0.1-8500"]
    o.a.c.c.StandardService      : Starting service [Tomcat]
    o.a.c.c.StandardEngine       : Starting Servlet engine: [Apache Tomcat/9.0.37]
    
    我怀疑这种变化:
  • 从 8.5.51 开始,AJP 连接器的默认监听地址改为环回地址而不是所有地址。

  • 是什么导致了我这个问题,但我不知道如何解决它。

    最佳答案

    我在升级 tomcat 版本时遇到了类似的问题。将下面提到的属性添加到 ajp 连接器有助于我的情况。

    connector.setProperty("address","0.0.0.0");
    connector.setProperty("allowedRequestAttributesPattern",".*");
    ((AbstractAjpProtocol)connector.getProtocolHandler()).setSecretRequired(false);
    
    详细说明:
    对于你的疑问:

    In 8.5.51 onwards, the default listen address of the AJP Connector was changed to the loopback address rather than all addresses.


    在此更新之前,tomcat AJP 连接器愿意接受来自任何 IP 地址的请求,因此不需要明确指定“地址”属性。但是在此更新之后,默认行为是 AJP 连接器愿意接受仅作为 localhost(环回)发出的请求。使用下面列出的“地址”属性将监听范围扩展到不仅是环回地址
    connector.setProperty("address","0.0.0.0"); // OR connector.setProperty("address","::");
    
    使用以下属性启用所有类型的请求属性(除非您有 header 信息,在这种情况下启用特定的)。具有无法识别的请求属性的请求将被拒绝并返回 403 响应:
    connector.setProperty("allowedRequestAttributesPattern",".*"); 
    
    使用“secretRequired”属性来定义是否需要与 HTTP 服务器交换 secret 以允许通过 ajp 的请求。如果是,则还要设置“secret”属性。否则请求将失败并显示 403。
    ((AbstractAjpProtocol)connector.getProtocolHandler()).setSecretRequired(false);
    
    引用:Apache Tomcat 8 Configuration Reference

    Use of the AJP protocol requires additional security considerations because it allows greater direct manipulation of Tomcat's internal data structures than the HTTP connectors. Particular attention should be paid to the values used for the address, secret, secretRequired and allowedRequestAttributesPattern attributes.

    关于spring-boot - Apache 无法连接到新的 tomcat 9 ajp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63505670/

    相关文章:

    java - Spring Boot 2.0.4 + OAuth2 + JWT - 无法获取访问 token ,返回 405 或只是映射到 localhost :8080/

    java - 使用servlet和html在服务器上上传文件时找不到符号getServletContext()错误

    java - 在 tomcat 中使用上下文条目运行 Web 应用程序

    java - 仅在 REST API 调用时出现 Spring Boot 404

    java - 如何将我的 JAR 库添加到 ubuntu 中的 tomcat7 安装

    spring-boot - 此构建中使用了 Gradle 功能,使其与 Gradle 7.0 不兼容

    java - Spring 测试对于不安全的 URL 返回 401

    spring-boot - 通过MultiHttpSecurityConfig实现 "oauth2Login()"和 "httpBasic()"同一页面

    apache - 将支付信息发送到服务器的最佳方式是什么?

    java - 使用 Maven 构建 Hadoop 时出错