You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache2如何将WebSocket代理至HTTP/2后端?问题求助

问题描述

我的Node.js应用同时兼容HTTP/1和HTTP/2服务器。我已配置Apache2作为正向代理,HTTP/1模式下与后端配合完全正常:

# 重定向所有请求到HTTP/1服务器
RewriteEngine  on
RewriteCond %{HTTP:Upgrade} !websocket [NC]
RewriteCond %{HTTP:Connection} !upgrade [NC]
RewriteRule    "^/(.*)$"  "http://localhost:3002/$1"  [P]

RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:3002/$1" [P,L]

但将应用切换到HTTP/2模式并配置Apache2代理HTTP/2流量时出现问题:

# 重定向所有请求到HTTP/2服务器
RewriteEngine  on
RewriteCond %{HTTP:Upgrade} !websocket [NC]
RewriteCond %{HTTP:Connection} !upgrade [NC]
RewriteRule    "^/(.*)$"  "h2c://localhost:3002/$1"  [P]

RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:3002/$1" [P,L]

使用h2c协议时静态文件加载正常,但WebSocket完全无法工作。

已加载的Apache模块:

# apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 headers_module (shared)
 http2_module (shared)
 mime_module (shared)
 mpm_event_module (shared)
 negotiation_module (shared)
 proxy_module (shared)
 proxy_fcgi_module (shared)
 proxy_http_module (shared)
 proxy_http2_module (shared)
 proxy_wstunnel_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 socache_shmcb_module (shared)
 ssl_module (shared)
 status_module (shared)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 07:17:34