在生产环境中配置Apache反向代理以托管Laravel Echo服务器

5

我需要托管一个使用laravel-echo-server的laravel应用程序到HTTPS服务器上。

我想使用Apache的反向代理将我的/socket.io URL轮询重定向到同一域名URL中的端口为6001127.0.0.1,该地址是laravel-echo-server运行的位置。

例如,在https://example.com上,当我的laravel-echo-server发送URL轮询到https://example.com/socket.io时,Apache应将其重定向到同一域名下的http://127.0.0.1:6001

注意

我没有将我的laravel应用程序托管在根目录中,而是在cpanel中的子目录中。

我的服务器是一个VPS,我正在从独立于主服务器主机的单独IP地址的子域主机上进行托管。

假设我的主机是指向/home/...目录的具有唯一IP地址的host.mydomain.com。这运行在http上。

现在我有一个具有唯一IP地址的domestic.mydomain.com,它指向/home/domestic/...目录,我正在从那里托管我的laravel应用程序。这运行在https上。

但是,我从host.mydomain.com IP地址登录cpanel,在那里访问domestic.mydomain.com文件管理器。

我的laravel-echo-server.json长这样:

{
    "authHost": "https://example.com",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "xxxx",
            "key": "xxxxxxxx"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": false,
    "host": "127.0.0.1",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

我如何部署我的 laravel-echo:
import Echo from "laravel-echo";
window.io = require('socket.io-client');
// Have this in case you stop running your laravel echo server
if (typeof io !== 'undefined') {
  window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname,
  });

  window.Echo.channel('session-expired')
  .listen('sessionExpired', (e) => {
    setTimeout(location.reload(), 3000);
  });
}

当我运行laravel-echo-server start时,它成功地初始化并显示正在127.0.0.1上的端口6001上运行

然后这里是我的Apache反向代理配置/etc/httpd/conf/httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
# mod_proxy setup.
ProxyRequests Off
ProxyPass /socket.io http://127.0.0.1:6001
ProxyPassReverse /socket.io http://127.0.0.1:6001
<Location "/socket.io">
  Order allow,deny
  Allow from all
</Location>

我的问题是当我打开网站时,投票同时返回一组OK404响应,但它没有加入我的socket.io中的任何频道。经过大量测试,我发现代理确实重定向了,但我怀疑它是否命中了我想要的特定响应。
以下是我的404的HTML响应,这不是我的服务器配置的404响应:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>

在我的本地服务器上它运行得非常完美,但我需要让它在生产服务器上运行。

1个回答

2

终于搞定了。

虽然我的网站仍然必须拥有SSL,但解决方案是让Apache/socket.io重定向到已配置为redishttp://localhost:6001/socket.io。然后使用2.2.3版本的socket io

所以我的laravel-echo-server.json没有为SSL配置。

这是我的laravel-echo-server.json

{
   "authHost": "https://domainName.com",
   "authEndpoint": "/broadcasting/auth",
   "clients": [
      {
        "appId": "xxxxxxx",
        "key": "xxxxxxxxxxx"
      }
   ],
   "database": "redis",
   "databaseConfig": {
      "redis": {},
      "sqlite": {
          "databasePath": "/database/laravel-echo-server.sqlite"
      }
   },
   "devMode": false,
   "host": null,
   "port": "6001",
   "protocol": "http",
   "socketio": {},
   "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"
}

"最初的回答"
我如何使用laravel-echo-server.json:
import Echo from "laravel-echo";
window.io = require('socket.io-client');
// Have this in case you stop running your laravel echo server
if (typeof io !== 'undefined') {
  window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname,
  });

}

我的SSL virtualhost 的apache配置如下:

最初的回答:

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
RewriteCond %{QUERY_STRING} transport=websocket    [NC]
RewriteRule /(.*)           ws://localhost:6001/$1 [P,L]

ProxyPass        /socket.io http://localhost:6001/socket.io
ProxyPassReverse /socket.io http://localhost:6001/socket.io 

此外,还需要一个Node.js进程管理器来保持laravel-echo-server运行。因此我创建了echo-server.json并放置了以下代码。

{
  "name": "apps",
  "script": "laravel-echo-server",
  "args": "start"
}

接下来,我安装了pm2进程管理器。使用npm install pm2 -g命令,并启动了我的服务pm2 start echo-server.json --name="apps"
最后,我使用pm2 list查看所有服务,并使用pm2 startup让它们一直运行。

嘿,你能帮我一下吗?我正在遇到很多困难:( 我的Laravel队列监听器捕获事件,我发现在PM2控制台中内存使用量正在增加,但是没有任何响应,什么都不起作用。 - Tyoma Inagamov
我之前手动启动了laravel-echo-server,但它没有响应。用户从未加入任何频道。 - Tyoma Inagamov
我看到事件正在到来 php artisan queue:listen --timeout=0,但它没有在 laravel-echo-server 上出现。也没有用户加入他的频道。 - Tyoma Inagamov
@TyomaInagamov 首先,这个配置是针对 Apache 而不是 nginx。其次,它是在私有通道上吗?尝试使用公共通道,让我们看看。最后,laravel-echo-server 只在 socket io 的 2 版本上为我工作。在 GitHub 上已经开了一个问题 https://github.com/tlaverdure/laravel-echo-server/issues/559 - Ikechukwu
我使用私有频道。公共频道的情况似乎也是一样的,无法工作。 - Tyoma Inagamov
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接