[英]Socket.io with connection on node server with HTTPS and NGINX
I'm trying to work with the socket but when connecting the system in production, I believe that due to SSL certificates it ends up giving some conflict and this error pops:我正在尝试使用套接字,但是在生产中连接系统时,我相信由于 SSL 证书,它最终会产生一些冲突,并且会弹出此错误:
Mixed Content: The page at ' https://myapp.com ' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint ' http://myapp.com:3001/socket.io/?EIO=3&transport=polling&t=N4Tk_Lq '.
混合内容:“ https://myapp.com ”页面已通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点“ http://myapp.com:3001/socket.io/?EIO=3&transport=polling&t=N4Tk_Lq ” . This request has been blocked;
此请求已被阻止; the content must be served over HTTPS.
内容必须通过 HTTPS 提供。
I tried several solutions found on the internet, but none of it worked so I asked the question here.我尝试了在互联网上找到的几种解决方案,但都没有奏效,所以我在这里问了这个问题。
Config on back-end:后台配置:
const clientServer = require("http").Server(client.app);
global.io = require('./socketio.js').init(clientServer, {
pingInterval: 10,
pingTimeout: 5,
});
clientServer.listen(3001, () => {
console.log("client ON");
});
Config on front-end:前端配置:
const config: SocketIoConfig = { url: 'http://myapp.com:3001', options: {} };
Nginx: nginx:
location /client/v1 {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
If NGINX is acting as a reverse proxy, then the port 3001
is never exposed.如果 NGINX 充当反向代理,则永远不会公开端口
3001
。
Try appending the /client/v1
endpoint to your domain name in the config on the front-end:尝试在前端的配置中将
/client/v1
端点附加到您的域名:
const config: SocketIoConfig = { url: 'https://myapp.com/client/v1', options: {} };
Or, if you need to include the .br
...或者,如果您需要包含
.br
...
const config: SocketIoConfig = { url: 'https://myapp.com.br/client/v1', options: {} };
I managed to put the node server to https, changing also in nginx我设法将节点服务器置于 https,也在 nginx 中更改
on the angular client side it looked like this (front-end)在角度客户端它看起来像这样(前端)
const config: SocketIoConfig = { url: 'https://myapp.com:3001', options: {secure: true} };
for some reason it didn't work with https://myapp.com/client/v1
出于某种原因,它不适用于
https://myapp.com/client/v1
on the server side with nodejs it looks like this:在带有 nodejs 的服务器端,它看起来像这样:
var privateKey = fs.readFileSync('/etc/letsencrypt/live/myapp.com/privkey.pem', 'utf8').toString();
var certificate = fs.readFileSync('/etc/letsencrypt/live/myapp.com/cert.pem', 'utf8').toString();
var chain = fs.readFileSync('/etc/letsencrypt/live/myapp.com/chain.pem').toString();
var options = { key: privateKey, cert: certificate, chain: chain };
Starting server like that:像这样启动服务器:
const clientServer = require("https").Server(options,client.app);
global.io = require('./socketio.js').init(clientServer, {
pingInterval: 10,
pingTimeout: 5,
});
clientServer.listen(3001, () => {
console.log("client ON");
});
and in nginx I had to put the "s" in the proxy_pass:在 nginx 中,我必须将“s”放在 proxy_pass 中:
location /client/v1 {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.