[英]Bad Gateway Error using Socket.io Node.js over SSL
I previously had a Socket.io script running fine over http, but upgrading to https has broken it. 我以前在Http上运行的Socket.io脚本运行良好,但是升级到https会破坏它。 I have installed the cert on the server but no luck.
我已经在服务器上安装了证书,但是没有运气。 The code for the server setup is:
服务器设置的代码是:
var https = require('https'),
fs = require('fs');
var options = {
key: fs.readFileSync('/etc/nginx/ssl/default/54082/server.key'),
cert: fs.readFileSync('/etc/nginx/ssl/default/54082/server.crt')
};
var app = https.createServer(options);
var io = require('socket.io').listen(app);
However in the web browser the page fails to connect to it and the console shows a the server responded with a status of 502 (Bad Gateway)
response. 但是,在Web浏览器中,页面无法连接到该页面,控制台显示
the server responded with a status of 502 (Bad Gateway)
并显示the server responded with a status of 502 (Bad Gateway)
。
Any ideas on if the script set up is wrong? 关于脚本设置是否有任何想法? Or perhaps something in the Nginx setup?
还是Nginx设置中的某些内容?
Many thanks 非常感谢
Edit: The front end code I'm using to connect: 编辑:我用来连接的前端代码:
<script type="text/javascript" src="https://socket.example.com/socket.io/socket.io.js"></script>
<script>
var io = io('https://socket.example.com', { secure: true });
</script>
Edit:: Nginx config: 编辑:: Nginx配置:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/before/*;
server {
listen 443 ssl;
server_name socket.example.co.uk;
root /home/forge/socket.example.co.uk;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/server/*;
location / {
proxy_pass https://socket.example.co.uk:3000;
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;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/after/*;
nginx
config attempting to proxy to the port you are running your socket.io server on. nginx
配置中删除任何位置块,以尝试代理到正在运行socket.io服务器的端口。 io.connect()
instead of the way you are doing. io.connect()
连接,而不是执行操作。 leave out the protocol portion of the url ( https://
). https://
)。 sudo killall -9 node
from the commandline to kill any zombie processes that might be lingering and bound to your port. sudo killall -9 node
使用sudo killall -9 node
杀死可能会缠结并绑定到您端口的任何僵尸进程。 this sometimes happens with socket.io when it fails to shutdown properly. example from my own code: 我自己的代码中的示例:
var socket = io.connect('dev.somedomain.com:3000',{secure:true});
server example from my own domain: 我自己的域中的服务器示例:
var fs = require('fs'),
https = require('https'),
config = JSON.parse(fs.readFileSync('./config.json','utf-8')),
serverOpts = {
key: fs.readFileSync(config.server.key),
cert: fs.readFileSync(config.server.cert)
},
server = https.createServer(serverOpts,function(req,res){}),
io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log('houston, we have lift off');
});
server.listen(config.port, function(){
log('listening on *:%d',config.port);
});
obviously i'm reading the path to my certificate and key file from a config.json file but you should get the idea right? 显然,我正在从config.json文件中读取证书和密钥文件的路径,但是您应该正确了吗?
The 502 (Bad Gateway) indicates that the nginx service tried to contact the proxy server but failed. 502(错误网关)指示nginx服务尝试联系代理服务器但失败。 The line in the nginx config
Nginx配置中的行
proxy_pass https://socket.example.co.uk:3000;
seems to be the issue. 似乎是问题所在。 Could not see from your nodejs code that the port 3000 is being used.
从您的nodejs代码中看不到端口3000正在使用。
我有同样的问题,但是使用apache2,我通过将这行添加到我的vhost配置中解决了这个问题
SSLProxyEngine on SSLProxyVerify none SSLProxyCheckPeerCN off SSLProxyCheckPeerName off SSLProxyCheckPeerExpire off
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.