Laravel Websockets Apache2 反向代理 设置

4
问题

我正在尝试在apache服务器后面设置一个带有laravel websockets库的实时环境。 Websocket服务器在端口6001上运行(从外部无法访问)。 Apache VHost已配置为ws.example.com

我无法使Apache正确代理wss:// 请求。 对wss://ws.example.com/request/path?protocol=7&client=js&version=5.1.1&flash=false 的请求失败了。 (Error during WebSocket handshake: Invalid status line)

我认为我的Vhost配置存在问题。 我是否漏掉了什么? 任何建议都将不胜感激。

vhost配置
<VirtualHost *:443>
    ServerName ws.example.com
    ServerAlias www.ws.example.com.com
    DocumentRoot /srv/vhost/example.com/domains/ws.example.com/public_html

    ErrorLog /var/log/virtualmin/ws.example.com_error_log
    CustomLog /var/log/virtualmin/ws.example.com_access_log combined
    ScriptAlias /cgi-bin/ /srv/vhost/example.com/domains/ws.example.com/cgi-bin/

    DirectoryIndex index.php index.html

    RewriteEngine on
    ProxyRequests off
    ProxyVia on
    RewriteCond %{HTTP:Connection} Upgrade [NC]
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteRule /(.*) ws://localhost:6001/$1 [P,L]
    ProxyPass               /request/path http://localhost:6001/request/path
    ProxyPassReverse        /request/path http://localhost:6001/request/path

    SSLCertificateFile /etc/letsencrypt/path/ws.example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/path/ws.example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
2个回答

2
创建一个用于WebSockets的子域名。然后按照以下方式编辑您的虚拟主机配置(Apache 2.4)。使用pusher-php-server 5.0.3版本。
<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName socket.website.com

    <Proxy *>
        Require all granted
        Allow from all
    </Proxy>

    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off

    RewriteEngine on
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule .* wss://127.0.0.1:6001%{REQUEST_URI} [P]
    ProxyPass / ws://127.0.0.1:6001
    ProxyPassReverse / ws://127.0.0.1:6001

    SSLCertificateFile /etc/letsencrypt/live/socket.website.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/socket.website.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

0

@max:你的重写规则是关键,当代理只转发未加密流量且Apache处理外部SSL时也适用,将wss替换为ws,经过一天的摆弄终于终于成功了!

编辑:评论需要更多声望,抱歉。


1
作为提示,将非答案的内容发布为答案会冒险遭到负评,这将使您失去积分,并使您更难以发表评论。 - camille

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接