laravel - Websocket未连接 - Docker
问题描述
我让 Laravel 在 Docker 环境中运行,包 mysql、nginx 和 php-fpm。我将它用作 API。我想添加 websocket,服务器是通过 php artisan websocket:serve 使用 supervisor 启动的。我无法连接到统计 laravel-websockets,我在控制台上看不到任何乒乓球。
我究竟做错了什么 ?
广播.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,
],
]
容器
解决方案
据我所知,您的 websocket 服务器与您的 php 服务器不在同一个容器中,因此当您尝试连接到 localhost 时它会失败。
如果您想连接到不同的容器,您应该使用该容器的名称而不是 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
],
],
推荐阅读
- python - 如何判断字符串是路径还是url?
- java - 在尝试仅检索不包括子字段的父字段时减少 json 输出
- javascript - 基于扩展运算符更改参数的值
- javascript - 你能解释一下为什么按钮在小班上执行相同的结果吗?
- sql - 错误:在或附近的语法错误| modify('替换 | PostgresSql 的值
- python - 如何从 Django 为实时服务器调用 postgresql 函数?
- javascript - Spartacus SSR 模式:在组件上应用 AuthGuard 后,登录屏幕会在刷新时暂时显示,尽管用户已经登录
- excel - 使用 VBA/SQL 返回 Access 数据库值
- r - 通过插入两个原始数据帧中每一个的列将两个数据帧合并为一个
- excel - 如何将exel打印为PDF,不包括第一张纸