【问题标题】:Websocket not connecting - DockerWebsocket未连接 - Docker
【发布时间】:2021-10-13 13:38:37
【问题描述】:

我让 Laravel 在 Docker 环境中运行,包 mysql、nginx 和 php-fpm。 我将它用作 API。 我想添加 websocket,服务器是通过 php artisan websocket:serve 使用 supervisor 启动的。 我无法连接到统计 laravel-websockets,我在控制台上看不到任何乒乓球。

我做错了什么?

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'),
            'scheme' => 'http',
            'host' => '127.0.0.1',
            'port' => 6001
        ],
    ],

ngnix.conf

map $http_upgrade $type {
default "web";
websocket "ws";
 }

server {
 listen 80 ;
 listen 444 ssl ;

client_max_body_size 520M;

access_log /var/log/nginx/application.access.log;
# mapping the entry point
root /application/Edidact_Backend/public;

# Deny all . files
location / {
    try_files $uri /index.php$is_args$args;
}
location @web  {
    try_files $uri $uri/ /index.php?$query_string;
  }
location @ws  {
  proxy_pass             http://php-fpm:6001;
  proxy_set_header Host  $host;
  proxy_read_timeout     60;
  proxy_connect_timeout  60;
  proxy_redirect         off;

# Allow the use of websockets
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}
# Handle Cache Files Automatically
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {
    expires 30d;
    add_header Cache-Control "max-age=0, must-revalidate";
}
# Handleing Php Files
location ~ ^/index\.php(/|$) {
    fastcgi_pass php-fpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_index app.php;
    send_timeout 1800;
    fastcgi_read_timeout 1800;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    include fastcgi_params;
}

location ~ \.php$ {
    return 404;
}
# Statics
location /(bundles|media) {
    access_log off;
    expires 30d;
    try_files $uri @rewriteapp;

}

}

websockets.php

'apps' => [
    [
        'id' => env('PUSHER_APP_ID'),
        'name' => env('APP_NAME'),
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'enable_client_messages' => false,
        'enable_statistics' => true,
    ],
]

容器

【问题讨论】:

    标签: laravel docker websocket laravel-websockets


    【解决方案1】:

    据我所知,您的 websocket 服务器与您的 php 服务器不在同一个容器中,因此当您尝试连接到 localhost 时它会失败。

    如果您想连接到不同的容器,您应该使用该容器的名称而不是本地主机,在您的情况下:

    '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'),
            'scheme' => 'http',
            'host' => 'php-fpm-service-V2', // Name of the php server
            'port' => 6001
        ],
    ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2021-10-17
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多