【问题标题】:httpd websocket error, 'upgrade' token not found in 'Connection' headerhttpd websocket 错误,在“连接”标头中找不到“升级”令牌
【发布时间】: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 \"%{Referer}i\" \"%{User-agent}i\"" 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

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

【问题讨论】:

    标签: javascript apache websocket virtualhost


    【解决方案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/
    

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2014-06-29
      • 2020-10-10
      • 1970-01-01
      • 2020-08-14
      • 2021-01-15
      • 2014-06-24
      • 2014-04-25
      • 2016-05-21
      相关资源
      最近更新 更多