简体   繁体   English

通过SSL使用Socket.io Node.js的网关错误

[英]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/*;
  1. make sure that your domain points to your server. 确保您的域指向您的服务器。
  2. make sure that an nginx server block is running for your domain with ssl enabled. 确保在启用了ssl的域中正在运行nginx服务器块。
  3. remove any location blocks from your nginx config attempting to proxy to the port you are running your socket.io server on. nginx配置中删除任何位置块,以尝试代理到正在运行socket.io服务器的端口。
  4. make sure your ssl certificate is valid. 确保您的ssl证书有效。
  5. connect with io.connect() instead of the way you are doing. 使用io.connect()连接,而不是执行操作。 leave out the protocol portion of the url ( https:// ). 省略网址的协议部分( https:// )。
  6. use 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. 当socket.io无法正常关闭时,有时会发生这种情况。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM