2015-07-12 80 views
1

我试图设置我的生产服务器使用nodejs和HTTPS使用faye消息,但没有运气。Faye与HTTPS nodejs

我直到现在是:

一个王菲+的NodeJS服务器安装文件:

var https = require('https'); 
var faye = require('faye'); 
var fs = require('fs'); 

var options = { 
    key: fs.readFileSync('/etc/httpd/ssl/example.com.key'), 
    cert: fs.readFileSync('/etc/httpd/ssl/example.com.crt'), 
    ca: fs.readFileSync('/etc/httpd/ssl/ca_bundle.crt') 
}; 

var server = https.createServer(options); 
var bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 60}); 

bayeux.attach(server); 
server.listen(8000); 

Rails的助手发送消息:

def broadcast(channel, &block) 
    message = {:channel => channel, :data => capture(&block)} 
    uri = URI.parse(Rails.configuration.faye_url) 
    Net::HTTPS.post(uri, message.to_json) 
end 

一javascript函数打开一个监听器:

function openListener(channel, callback){ 
    var faye_client = new Faye.Client("<%= Rails.configuration.faye_url %>"); 
    faye_client.subscribe(channel , callback); 
    return faye_client; 
} 

我王菲URL配置在production.rb:

config.faye_url = "https://example.com:8000/faye" 

最后,在我的页面中调用JavaScript:在测试时

fayeClient = openListener("my_channel" , function(data) { 
    //do something... 
}); 

一切工作HTTP在开发机器上。但在生产中不。

如果我点浏览器到https://example.com:8000/faye.js我得到了正确的JavaScript文件。

会发生什么?

+0

您的证书是否自签名?我不知道Ruby API,但在制作HTTPS帖子时,在大多数API中,可以选择* not *来验证HTTPS证书,因为证书是自签名的。 – Ankur

+0

是的。我从Godaddy得到 – Beetlejuice

回答

3

问题出在Apache服务器上。

我已经切换到nginx,现在它的工作。

不过,我需要做一些配置:

王菲+ Node.js的安装文件:

var http = require('http'), 
    faye = require('faye'); 

var server = http.createServer(), 
    bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 60}); 

bayeux.attach(server); 
server.listen(8000); 

Rails的助手:

def broadcast(channel, &block) 
    message = {:channel => channel, :data => capture(&block)} 
    uri = URI.parse(Rails.configuration.faye_url) 
    Net::HTTP.post_form(uri, :message => message.to_json) 
end 

王菲网址:

https://example.com/faye 

最后,nginx的配置

server { 
    # Listen on 80 and 443 
    listen 80; 
    listen 443 ssl; 
    server_name example.com; 
    passenger_enabled on; 
    root /home/rails/myapp/public; 

    ssl_certificate /home/rails/ssl/myapp.crt; 
    ssl_certificate_key /home/rails/ssl/myapp.key; 

    # Redirect all non-SSL traffic to SSL. 
    if ($ssl_protocol = "") { 
      rewrite^https://$host$request_uri? permanent; 
    } 

    location /faye { 
     proxy_pass http://127.0.0.1:8000; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
    } 
} 

短词:nginx的转换在客户端的HTTPS请求在/王菲地址,在端口HTTP网站的服务器端8000 使用默认HTTP和HTTPS侧。