2015-07-12 100 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側。