无法连接到 cpanel https 上的套接字 io 服务器
Cant connect to socket io server on cpanel https
我在服务器上安装了 whm 并创建了 2 个主机
1。 app.example.com
2. 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/
我可以从本地主机连接到 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.com
和 app.example.com
共享同一个域,并且 socket.example.com
不允许跨源请求,所以当您尝试从本地主机连接时抛出错误。尝试在 socket.example.com's
虚拟主机中将本地主机列入白名单或尝试以下解决方案。
试试这个。
在您的虚拟主机文件中,将 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
访问您的本地应用程序。
它应该有效。
我在服务器上安装了 whm 并创建了 2 个主机
1。 app.example.com 2. 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/
我可以从本地主机连接到 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.com
和 app.example.com
共享同一个域,并且 socket.example.com
不允许跨源请求,所以当您尝试从本地主机连接时抛出错误。尝试在 socket.example.com's
虚拟主机中将本地主机列入白名单或尝试以下解决方案。
试试这个。
在您的虚拟主机文件中,将 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
访问您的本地应用程序。
它应该有效。