小编典典

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

redis

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

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

例如,https://example.com当我laravel-echo- serverhttps://example.com/socket.ioapache
发送url民意调查时,应将其重定向到http://127.0.0.1:6001同一域内。

注意

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

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

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

现在,我有一个domestic.mydomain.com指向/home/domestic/...目录的唯一IP地址,这是我托管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>

它可以在我的本地服务器上完美运行,但是我需要使其在我的生产服务器上运行。


阅读 290

收藏
2020-06-20

共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,
  });

}

virtualhost我的域的SSL中的我的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
2020-06-20