Laravel Websockets Apache2 ReverseProxy 设置

标签 laravel apache websocket reverse-proxy laravel-websockets

问题
我正在尝试在 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>

最佳答案

为 websockets 创建一个子域。然后编辑您的虚拟主机配置(Apache 2.4),如下所示:

<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>
broadcasting.php
    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => env('PUSHER_APP_CLUSTER'),
            'host' => '127.0.0.1',
            'encrypted' => true,
            'port' => 6001,
            'scheme' => 'https',
            'curl_options' => [
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
            ]
        ],
    ],
websockets.php
'dashboard' => [
    'port' => env('LARAVEL_WEBSOCKETS_PORT', 443) // <- we changed this to 443
],
'apps' => [
    [
        'id' => env('PUSHER_APP_ID'),
        'name' => env('APP_NAME'),
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'enable_client_messages' => true,
        'enable_statistics' => true,
        'encrypted' => true,
        'host' => env('WEBSOCKETS_URL') // for dashboard
    ],
],
'allowed_origins' => [
    parse_url(env('APP_URL'))['host']
],
'statistics' => [
    ...
    'perform_dns_lookup' => true, // For statistics to work
    ...
],
'ssl' => [
    'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
    'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
    'passphrase' => null,
    'verify_peer' => false
],
.env
WEBSOCKETS_URL=socket.website.com
LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT=/etc/letsencrypt/live/socket.website.com/fullchain.pem
LARAVEL_WEBSOCKETS_SSL_LOCAL_PK=/etc/letsencrypt/live/socket.website.com/privkey.pem
/etc/supervisord.d/websockets.conf
[program:websockets]
command=php /var/www/html/website.com/artisan websockets:serve --host=127.0.0.1 --port=6001
process_name=websockets
numprocs=1
autostart=true
autorestart=true
Echo
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: window["MIX_PUSHER_APP_KEY"], // <- from .env
    wsHost: window["WEBSOCKETS_URL"], // <- from .env
    wsPort: 80,
    wssPort: 443,
    disableStats: true, 
    enabledTransports: ['ws', 'wss']
});

关于Laravel Websockets Apache2 ReverseProxy 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62792517/

相关文章:

apache - 哈多普 : supporting multiple outputs for Map Reduce jobs

apache - 将子域名重定向到相同子域名但不同域名

android - 无法在 android socketio 客户端与 socketio 服务器之间建立连接

laravel - 如何统计当年每个月的文章数?

php - Laravel View Make 返回空白页

php - 文档中的最后一张图像无法加载 - "Failed to Load Resource"

websocket - 使用 Sec-WebSocket-Key 来识别 websocket 客户端连接是个好主意吗?

javascript - 戈朗 : dealing with binary data

php - 为什么 Laravel 在每个 Artisan 命令中都不断调用 schedule()?

validation - 使用 Laravel 验证规则检查用户的年龄