You need to enable JavaScript to run this app. 如何为多Spring Boot应用配置带路径前缀的Apache负载均衡
优惠活动
大模型
产品
解决方案
定价
更多

如何为多Spring Boot应用配置带路径前缀的Apache负载均衡

Apache多路径前缀负载均衡配置实现指南

原配置基础错误修正

原WebSocket集群的BalancerMember写法存在协议拼接错误,ws://http://server1:port多了http://前缀,会直接导致WebSocket代理失败,正确格式应为ws://server1:port。

多路径前缀分组转发配置改写

要实现/app1、/app2分别对应不同后端服务集群的需求,直接按路径前缀匹配规则映射对应Balancer集群即可,完整配置参考:

# 粘性会话Cookie配置
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

# app1 对应HTTP后端集群
<Proxy "balancer://app1_cluster">
    BalancerMember "http://server1:port1" route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    BalancerMember "http://server2:port1" route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    ProxySet stickysession=ROUTEID
</Proxy>

# app1 对应WebSocket后端集群
<Proxy "balancer://app1_ws">
    BalancerMember "ws://server1:port1" route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    BalancerMember "ws://server2:port1" route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    ProxySet stickysession=ROUTEID
</Proxy>

# app2 对应HTTP后端集群
<Proxy "balancer://app2_cluster">
    BalancerMember "http://server1:port2" route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    BalancerMember "http://server2:port2" route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    ProxySet stickysession=ROUTEID
</Proxy>

# app2 对应WebSocket后端集群
<Proxy "balancer://app2_ws">
    BalancerMember "ws://server1:port2" route=1 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    BalancerMember "ws://server2:port2" route=2 keepalive=On smax=1 connectiontimeout=10 retry=600 timeout=900 ttl=900
    ProxySet stickysession=ROUTEID
</Proxy>

RewriteEngine On

# app1 转发规则
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^/app1/(.*)$ balancer://app1_ws/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^/app1/(.*)$ balancer://app1_cluster/$1 [P,L]

# app2 转发规则
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^/app2/(.*)$ balancer://app2_ws/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^/app2/(.*)$ balancer://app2_cluster/$1 [P,L]

规则匹配遵循“最长前缀匹配”原则,新增路径组只需按上述结构新增集群和对应Rewrite规则即可。

静态资源404问题解决

修改为ProxyPass "/test"后出现404的根源:
后端服务返回的HTML中,静态资源地址为根路径下的/static/css/app.xxx.css,浏览器会直接请求负载均衡根路径的/static资源,而非/test/static路径下的资源;当前仅配置了/test开头的转发规则,根路径的静态资源请求无法命中后端,因此返回404。

解决方式两种:

  • 调整后端应用上下文路径(推荐):将后端服务的访问上下文设置为/test,后端生成的静态资源地址会自动带上/test前缀,请求即可命中转发规则。
  • 负载均衡层补充静态资源转发规则:如果无法修改后端配置,可新增静态资源路径的映射规则,例如:
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule ^/(static/.*)$ balancer://mycluster/$1 [P,L]
    
    多个应用静态资源路径存在冲突时,需按应用前缀单独映射/test/static/这类带前缀的路径规则。

内容的提问来源于stack exchange,提问作者Kamienie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.29 04:45:02