sam*_*les 6 ssl nginx node.js socket.io
我之前有一个Socket.io脚本在http上运行正常,但升级到https已经打破了它.我在服务器上安装了证书,但没有运气.服务器设置的代码是:
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);
Run Code Online (Sandbox Code Playgroud)
但是,在Web浏览器中,页面无法连接到该页面,并且控制台显示the server responded with a status of 502 (Bad Gateway)响应.
关于脚本设置是否错误的任何想法?或者也许是Nginx设置中的某些东西?
非常感谢
编辑:我用来连接的前端代码:
<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>
Run Code Online (Sandbox Code Playgroud)
编辑:: 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/*;
Run Code Online (Sandbox Code Playgroud)
nginx尝试代理到您正在运行 socket.io 服务器的端口的任何位置块。io.connect()而不是你正在做的方式。省略 url ( https://) 的协议部分。sudo killall -9 node来杀死可能徘徊并绑定到您的端口的任何僵尸进程。当 socket.io 无法正常关闭时,有时会发生这种情况。我自己的代码示例:
var socket = io.connect('dev.somedomain.com:3000',{secure:true});
Run Code Online (Sandbox Code Playgroud)
来自我自己域的服务器示例:
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);
});
Run Code Online (Sandbox Code Playgroud)
显然我正在从 config.json 文件读取我的证书和密钥文件的路径,但您应该明白这个想法吗?
| 归档时间: |
|
| 查看次数: |
5333 次 |
| 最近记录: |