无法连接到cpanel https上的套接字io服务器



我在服务器上安装了whm并创建了2个主机

1。app.example.com2.socket.example.com

我在app.example.com上得到了我的laravel应用程序我正试图在socket.example.com 上创建套接字服务器

套接字服务器托管在socket.example.com 上

const app = require("express")();
const fs = require("fs")
const https = require("https")
const https_server = https.createServer({
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.cert')
},app);

const options = {
cors : {
origin : "*"
}
};
const io = require("socket.io")(https_server, options);
io.on("connection", socket => { /* ... */ });
https_server.listen(8001);

客户端托管在app.example.com 上

<script>
const socket = io("https://socket.example.com:8001",{
secure: true ,
}) ;
socket.on("connect",() => {
console.log("connected")
})
socket.on("message",( e ) => {
console.log( e )
})
</script>

apache配置用于反向代理

ProxyPreserveHost On
RewriteEngine On
# Everything else forwards as HTTP to the node app.
ProxyPass / https://127.0.0.1:8001/
ProxyPassReverse / https://127.0.0.1:8001/

我可以从localhost连接到socket.example.com:8001,但我不能从app.example.com连接

错误

Access to XMLHttpRequest at 'https://socket.example.com:8001/socket.io/?EIO=3&transport=polling&t=NcjrB8N' from origin 'https://app.example.com' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
connect_error due to xhr poll error

这是因为socket.example.comapp.example.com共享同一个域,而socket.example.com不允许跨源请求,所以当您尝试从localhost连接时会抛出错误。尝试在socket.example.com's虚拟主机中将localhost列入白名单,或尝试以下解决方案。

试试这个。

在您的虚拟主机文件中,将ServerName从localhost更改为test.example.com,或者创建如下虚拟主机文件(如果名称不为test.example.com(。

NameVirtualHost *:80
<VirtualHost *:80>
ServerName test.example.com
DocumentRoot /path/to/project/doc/root
ErrorLog "var/log/error_log"
CustomLog "var/log/access_log" common
</VirtualHost>

并在文件/etc/hosts的底部添加以下行

127.0.0.1 test.example.com

然后尝试从浏览器使用test.example.com访问您的本地应用程序。它应该起作用。

最新更新