【问题标题】:How to launch laravel-echo server on production?如何在生产中启动 laravel-echo 服务器?
【发布时间】:2021-04-20 12:34:33
【问题描述】:

我的产品。堆栈:

  1. Vue.js CLI - https://example.com
  2. Laravel API - https://example.com/api

我已经在我的本地主机上使用 laravel-echo 服务器socket.io 创建了一个实时聊天。
但是,我想运行它在我的生产服务器上。为此,我使用 nginx。

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

我已经重建了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 文件:

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"
    }
}

【问题讨论】:

    标签: nginx redis socket.io laravel-echo


    【解决方案1】:

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

    【讨论】:

    • 它没有抛出 404 或 502 有点奇怪,但它的作用几乎相同:没有错误,没有响应 :(
    • 从这个答案stackoverflow.com/a/56934080/5929919 的想法是在https 上设置它,但不要超过https,,而是让Apache 将此/socket.io 重定向到http://localhost:6001/socket.io 和基本上与该答案的唯一区别是我使用的是web.php 而不是api.php 路由。
    • 正确检查该答案的laravel-echo-server.json"protocol": "http", 但你的是 "protocol": "https","sslCertPath": "","sslKeyPath": "", 但在你的 "sslCertPath": "/etc/ssl/certs/nginx-selfsigned.crt","sslKeyPath": "/etc/ssl/private/nginx-selfsigned.key",
    • 我相信您的已配置为 SSL。您的解决方案与此不完全相同。该解决方案未针对 SSL 进行配置。虽然您的网站仍必须使用 SSL,但该解决方案是让 Apache 将 /socket.io 重定向到已为 redis 配置的 http://localhost:6001/socket.io。然后使用socket io的版本2.2.3
    • 如果此解决方案适合您。不要忘记标记两个答案并接受。
    猜你喜欢
    • 2016-04-30
    • 2017-01-11
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2012-05-30
    • 2019-09-13
    相关资源
    最近更新 更多