2015-09-01 48 views
3

我正在嘗試使用ActionCable(主要是複製DHH example)並嘗試使其在具有瘦(在端口8443上)和nginx的Ubuntu服務器上運行。這一切都在本地正常工作,但是,當我嘗試將其代理服務器上的活動服務器時,我得到此錯誤響應:failed: Error during WebSocket handshake: Unexpected response code: 301將nginx配置爲代理瘦和Rails ActionCable

這裏是我的我的nginx的配置的相關位:

server { 
    listen 80; 

    server_name _not_; 
    root /home/ubuntu/www/; 
} 

map $http_upgrade $connection_upgrade { 
    default upgrade; 
    '' close; 
} 

upstream websocket { 
    server 127.0.0.1:8443; 
} 

server { 

    listen 80; 

    ... 

    location /websocket/ { 
    proxy_pass http://127.0.0.1:8443; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection $connection_upgrade; 
    proxy_redirect off; 
    } 

    ... 

} 

我有點出我的聯賽nginx的這裏的 - 我錯過什麼或得到錯誤的?

+0

這是否會發生在60秒? – Anatoly

+0

@Anoly它在60秒內打開/關閉嗎?它根本不連接到websocket。 – Michael

+0

我不確定,我不認爲你的websockets服務器應該聽80. Il NGINX文檔,它聽8020. https://www.nginx.com/blog/websocket-nginx/ –

回答

1

我在一個月後回到了這裏,發現這些問題與nginx配置不同,但與瘦相關。我做了三件事情:

(1)配置薄使用the Faye adapter

# config.ru 

require 'faye' 
Faye::WebSocket.load_adapter('thin') 

require ::File.expand_path('../config/environment', __FILE__) 

use Faye::RackAdapter, mount: '/faye', timeout: 25 

run Rails.application 

(2)切換到routes.rb安裝ActionCable,而不是試圖運行它as a standalone

#routes.rb 

MyAwesomeApp::Application.routes.draw do 

    ... 

    match "/websocket", to: ActionCable.server, via: [:get, :post] 

end 

(3)回到我正常的nginx的配置,WebSockets的上游化薄(如Web服務器的功能:

map $http_upgrade $connection_upgrade { 
    default upgrade; 
    '' close; } 

upstream thin { 
    server 127.0.0.1:3000; 
} 

server { 
    ... 

    location /websocket { 
    proxy_pass http://thin; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection $connection_upgrade; } 

    ... 
} 

所以,我的道歉nginx的,我免除你 - 似乎問題被主要涉及薄


編輯:我已經添加了舊nginx的配置我在路由安裝後回到另外值得注意的,對於那些使用SSL是。將打破安全wss WebSocket。相反,您應該在控制器級別as recommended here上執行force_ssl,並配置nginx以將任何HTTP路由重寫爲HTTPS。

+0

你能當你在routes.rb中掛載時,保持你的問題中列出的nginx.conf類型相同? (2) –

+1

@DavidKuhta編輯答案,包括我返回的nginx配置,並給出一個SSL提示,導致我的問題。 – Michael

0

此線程對我非常有幫助,但我選擇將AC過程分隔成單獨的puma實例,以便可以分別配置工作人員等。後來我從nginx添加了SSL代理,以確保使用最新的密碼等。這可以避免rails/puma/AC不必擔心SSL與非SSL;服務器實例內的所有內容都是非SSL的。

這裏是我的服務器節AC:從這個github上issue

server { 
    listen 881 ssl; 


    ssl_certificate /etc/nginx/ssl/server.crt; 
    ssl_certificate_key /etc/nginx/ssl/server.key; 

    ssl_prefer_server_ciphers on; 
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; 


    server_name localhost; 
    location/{ 
     proxy_pass http://cable; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 
    } 
} 

注意:您需要確保您的AC配置可以讓你的域源:

#config/initializers/cable.rb 
ActionCable.server.config.allowed_request_origins = %w(http://my-domain.com )