redis 如何在生产环境中启动laravel-echo服务器?

lrpiutwd  于 2023-10-15  发布在  Redis
关注(0)|答案(2)|浏览(115)

我的产品堆栈:

  1. Vue.js CLI - https://example.com
  2. Laravel API - https://example.com/api
    我已经在我的本地主机上使用laravel-echo serversocket.io创建了一个实时聊天。
    但是,我想在我的生产服务器上运行它。我用的是nginx。

问题是,当我运行laravel-echo-server start时,它成功启动,但当我从Web浏览器访问我的网站时没有响应。(加入没有用

我已经重建了vue应用程序,清除了laravel缓存并重新启动了nginx服务。
/etc/nginx/sites-enabled/default中,我有:

server {
    server_name example.com www.example.com;

    root /var/www/html/Example/api/public/;    
    index index.html index.php;

    location / {
        root /var/www/html/Example/web/dist/;
        try_files $uri $uri/ /index.html;
    }

    location /socket.io/ {
        proxy_pass http://localhost:6001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
    }

    location /api {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
}

我的main.js文件,我连接到Web套接字:

import Echo from "laravel-echo"
window.io = require('socket.io-client')

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: 'https://example.com/socket.io',
    auth: {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('token')}`
        }
    }
});

我的laravel-echo.server.json

{
    "authHost": "https://example.com/api",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            appId: "xxxxx",
            key: "xxxxx"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}
6l7fqoea

6l7fqoea1#

尝试将此host: 'https://example.com/socket.io'更改为host: 'https://example.com'

agxfikkp

agxfikkp2#

我在laravel-echo配置中保留了普通https的站点,但我删除了ssl规则,如证书等。而不是指向localhost,我指向了主机ip,只改变了端口:

{
    "authHost": "https://example.com/api",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            appId: "xxxxx",
            key: "xxxxx"
        }
    ],
    "database": "redis",
    "databaseConfig": {
           "redis": {
                        "port": "6379",
                        "host": "127.0.0.1"
                },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": 127.0.0.1,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

在ngnix中添加:

location /socket.io {
                proxy_pass http://127.0.0.1:6001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }

在vue客户端中:

window.Echo = new Echo({
        broadcaster: 'socket.io',
        host: window.location.origin, // sem a porta
        auth: {
            headers: {
                Authorization: 'Bearer ' + bearerToken,
            }
        }
    });

相关问题