我正在运行 Laravel 7,并尝试使用 Nginx 代理和 SSL 运行 Laravel-Websockets。不幸的是,在我配置好一切之后,我遇到了以下问题:
WebSocket 连接至 'wss://www.rabter.com:6001/app/174e625ceea907e9e63c?protocol=7&client=js&version=4.3.1&flash=false' 失败:WebSocket 握手期间出错:意外的响应代码:502
在实施 SSL 之前,一切都正常工作。
/config/websockets.php
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
return [
'dashboard' => [
'port' => env('LARAVEL_WEBSOCKETS_PORT', 6001),
],
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY','174e625ceea907e9e63c'),
'secret' => env('PUSHER_APP_SECRET'),
'path' => env('PUSHER_APP_PATH'),
'capacity' => null,
'enable_client_messages' => true,
'enable_statistics' => true,
],
],
'app_provider' => BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider::class,
'allowed_origins' => [
//
],
'max_request_size_in_kb' => 250,
'path' => 'laravel-websockets',
'middleware' => [
'web',
'api',
Authorize::class,
],
'statistics' => [
'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class,
'interval_in_seconds' => 60,
'delete_statistics_older_than_days' => 60,
'perform_dns_lookup' => true,
],
'ssl' => [
'local_cert' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_CERT', null),
'local_pk' => env('LARAVEL_WEBSOCKETS_SSL_LOCAL_PK', null),
'passphrase' => env('LARAVEL_WEBSOCKETS_SSL_PASSPHRASE', null),
],
'channel_manager' => \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager::class,
];
`
/config/broadcasting.php
`
'default' => env('BROADCAST_DRIVER', 'pusher'),
'connections' => [
'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',
'port' => 6001,
'scheme' => 'https',
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
/etc/nginx/conf.d/vhosts/rabter.com.ssl.conf
listen 45.82.136.131:443 ssl;
server_name rabter.com;
return 301 https://www.rabter.com$request_uri;
}
server {
listen 45.82.136.131:443 ssl;
server_name www.rabter.com;
ssl_certificate /etc/pki/tls/certs/rabter.com.bundle;
ssl_certificate_key /etc/pki/tls/private/rabter.com.key;
root /home/rabter/core/public/;
index index.php;
access_log /var/log/nginx/rabter.com.bytes bytes;
access_log /var/log/nginx/rabter.com.log combined;
error_log /var/log/nginx/rabter.com.error.log error;
location / {
proxy_set_header Connection "keep-alive";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_pass https://45.82.136.131:3000$uri;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
proxy_intercept_errors on;
error_page 404 = @php;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
location @php {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 45.82.136.131:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
upstream websocket {
server 127.0.0.1:6001;
}
server {
listen 6001 ssl;
ssl_certificate /etc/myssl/certs/rabter.com.bundle;
ssl_certificate_key etc/myssl/private/rabter.com.key;
location / {
proxy_pass https://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_connect_timeout 43200000;
}
}
Laravel-Echo 配置
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
wsHost:'rabter.com',
wsPort:6001,
wssPort: 6001,
disableStats: true,
encrypted: true,
authEndpoint: process.env.CLIENT_URL + '/api/broadcasting/auth',
enabledTransports: ['ws', 'wss'],
}],
我正在使用nuxtjs作为前端技术,这个问题困扰了我一个多月。
非常感谢您的任何帮助。
listen 6001 ssl
,但你也有wsPort:6001
。尝试使用wsPort:6002
作为示例,然后在你的 nginx 配置中添加server 127.0.0.1:6002;
。但是请确保在从外部连接到 websocket 时连接到 6001(即 Laravel 配置可能保持不变)。 - apokryfoswsPort:6002
和wssPort:6002
,还有upstream websocket {server 127.0.0.1:6002;}
,我在location /
中使用它。重启nginx和npm后,我得到了WebSocket is closed before the connection is established.
的错误信息。但它一直在尝试连接,第二个错误始终是Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
。我还添加了proxy_send_timeout 43200000;proxy_read_timeout 43200000; proxy_connect_timeout 43200000;}
。 - Pc Monk