配置Apache反向代理以在生产中托管Laravel Echo Server

时间:2019-07-03 08:34:13

标签: laravel apache redis socket.io laravel-echo

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

我想使用Apache的反向代理将我的/socket.io URL轮询重定向到端口127.0.0.1上的6001laravel-echo-server在同一域URL中运行。

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

注意

我不是将laravel应用托管在cpanel的子目录中。

我的服务器是VPS,我从一个子域主机托管,该子域主机运行在与主服务器主机不同的IP地址上。

比方说,我的主要主机是host.mydomain.com,具有指向/home/...目录的唯一IP地址。它在http

上运行

我现在有一个domestic.mydomain.com,其IP地址指向/home/domestic/...目录,这是我托管laravel应用程序的位置。它在https上运行。

但是我的cpanel登录来自我访问host.mydomain.com文件管理器的domestic.mydomain.com IP地址

我的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时,它会成功初始化并显示running on 127.0.0.1 on port 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 个答案:

答案 0 :(得分:1)

最后知道了,这是我的laravel-echo-server.json

{
    "authHost": "https://example-domain.com",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "xxxxxxx",
            "key": "xxxxxxxxxxxxxx"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "port": "6379",
            "host": "localhost"
        },
        "sqlite": {}
    },
    "devMode": true,
    "host": "localhost",
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "/path/to/ssl/crt/crt.pem", // or .crt
    "sslKeyPath": "/path/to/ssl/key/crt.pem", // or .key
    "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,
  });

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

在我的域的SSL virtualhost中的我的apaxhe配置:

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