httpd websocket 错误,在“连接”标头中找不到“升级”令牌

Posted

技术标签:

【中文标题】httpd websocket 错误,在“连接”标头中找不到“升级”令牌【英文标题】:httpd websocket error, 'upgrade' token not found in 'Connection' header 【发布时间】:2022-01-09 14:32:23 【问题描述】:

我正在使用 Apache(httpd...Server version: Apache/2.4.37 (centos 8)) 在 Namecheap vps 上部署 Golang(go1.16.7) 后端 除了 websocket 连接之外,一切都有效。 请求:

wss://editor.xyzw.com/crownies/ws/upload/file?email=gbenroscience@gmail.com

当我尝试访问 websocket 连接时,它给出:

the client is not using the websocket protocol:'upgrade' token not found in 'Connection' header

(我在生产的 golang 代码中打印了这个错误)

websocket 在本地开发中工作,所以我知道 Golang 代码运行良好。 我确定问题出在我在 Apache 中的 vhost 配置中。

这是我的虚拟主机文件:

<VirtualHost *:443>
        ServerName editor.xyzw.com
        ServerAlias editor.xyzw.com www.editor.xyzw.com http://www.editor.xyzw.com
        UseCanonicalName Off
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%Refereri\" \"%User-agenti\"" combined
        CustomLog /var/log/httpd/editor_access.log combined
        ErrorLog /var/log/httpd/editor_error.log
        DocumentRoot /path/to/my/document/root
    
    SSLEngine on
    
    SSLCertificateFile /opt/ssl/cert.crt
    
    SSLCertificateKeyFile /opt/ssl/cert.key
    
    SSLCACertificateFile /opt/ssl/cert.ca-bundle
    
    
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
    
    
    
    
    RewriteEngine On
    RewriteCond %HTTP:Connection Upgrade [NC]
    RewriteCond %HTTP:Upgrade websocket [NC]
    RewriteRule ^/ws(.*) ws://localhost:8080/crownies/ws/$1 [P,L]
    
    ProxyRequests off
    <Location />
        ProxyPass http://localhost:8080/
        ProxyPassReverse http://localhost:8080/
    </Location>
    
    
    
    </VirtualHost>

来自 Google Chrome 的请求标头:

Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: Upgrade
Cookie: user=blah-blah-blah
Host: editor.xyzw.com
Origin: https://editor.xyzw.com
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: blah-blah-blah==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/96.0.4664.55 Safari/537.36

请问我的虚拟主机有什么问题?

【问题讨论】:

【参考方案1】:

我认为Location 优先于重写,因此当路径 重叠时,您必须坚持重写。不要忘记为后端设置适当的超时,例如示例(根据您的需要调整):

# Worker Options
<Proxy http://localhost:8080>
    ProxySet keepalive=On smax=1 connectiontimeout=10 retry=10 timeout=30 ttl=15
</Proxy>

<Proxy ws://localhost:8080>
    ProxySet keepalive=On smax=1 connectiontimeout=10 retry=10 timeout=900 ttl=15
</Proxy>

# Rewrites
RewriteEngine On

RewriteCond %HTTP:Connection Upgrade   [NC]
RewriteCond %HTTP:Upgrade    websocket [NC]
RewriteRule /ws/(.*) ws://localhost:8080/crownies/ws/$1 [P,L]

RewriteRule /(.*) http://localhost:8080/$1 [P,L]
ProxyPassReverse / http://localhost:8080/

【讨论】:

以上是关于httpd websocket 错误,在“连接”标头中找不到“升级”令牌的主要内容,如果未能解决你的问题,请参考以下文章

错误:在建立连接之前关闭了 WebSocket

Firefox 在连接错误时不会立即关闭 WebSocket?

AWS websocket在连接时抛出CORS错误

Django Channels 错误:失败:在建立连接之前关闭 WebSocket

当我在 Open*** 后面时,浏览器在建立 WebSocket 连接一秒后关闭并显示错误 1006

failed: WebSocket 在连接建立之前关闭