本文介绍了配置Apache反向代理以在生产中托管Laravel Echo Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to host a laravel application that utilizes laravel-echo-server over a HTTPS server.

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

I want to use Apache's reverse proxy to redirect my /socket.io url polls to 127.0.0.1 on port 6001 which is where laravel-echo-server is running on within the same domain url.

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

For example over https://example.com when my laravel-echo-server sends url polls to https://example.com/socket.io apache should redirect it to http://127.0.0.1:6001 within that same domain.

注意

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

I'm not hosting my laravel app at the root directory but in a subdirectory within my cpanel.

我的服务器是VPS,我是从子域主机托管的,该子域主机与主服务器主机的IP地址不同.

My server is a VPS and I am hosting from a sub domain host that runs on a separate IP address from that of the main server host.

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

Let's say my main host is host.mydomain.com with a unique IP address pointing to the /home/... directory. This runs on http

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

I now have a domestic.mydomain.com with a unique IP address pointing to the /home/domestic/... directory and this is where I'm hosting my laravel app from. This runs on https.

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

But my cpanel login is from the host.mydomain.com IP address where i access the domestic.mydomain.com file manager

我的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

And when I run laravel-echo-server start It successfully initialises and shows running on 127.0.0.1 on port 6001

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

Then here is my apache reverse proxy config /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中的任何通道.经过大量测试,我发现该代理确实可以重定向,但是我怀疑它是否达到了我想要的特定代理.

My problem is when I open my website the polls return a set of OK and 404 responses simultaneously, but it doesn't join any channels within my socket.io. After lots of testing i figured that the proxy does redirect but i doubt it's hitting the particular one I want.

这里是我的404的HTML响应,而不是服务器配置的404响应:

Heres the HTML responses for my 404, which is not my server's configured 404 response:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /</pre>
</body>
</html>

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

It runs perfectly on my local server but I need to make it run on my production server.

推荐答案

最后得到了,这是我的laravel-echo-server.json:

Finally got it, heres my 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:

How I utitlizes 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中的我的apaxhe配置:

And my apaxhe config within my SSL virtualhost for my domain:

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

这篇关于配置Apache反向代理以在生产中托管Laravel Echo Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:55