2013-04-13 2 views
7

Ruby on Rails 3.2 앱에 nginx 리버스 프록시 뒤에있는 씬 웹 서버에서 실행중인 websocket-rails gem이 있습니다.씬 + Nginx + 웹 소켓 구성 | Rails

nginx 역방향 프록시를 제외하고 모든 것이 올바르게 작동합니다. nginx 역방향 프록시를 제거하면 websocket 통신이 제대로 작동합니다. (개발 및 생산 모두). nginx를 웹 소켓의 리버스 프록시로 사용하면 문제가 시작됩니다.

Nginx 버전 1.3.13 이상에서 websocket 프록시를 지원할 수 있어야합니다.

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

upstream ravecy { 
    server localhost:3000; 
    server localhost:3001; 
} 

server { 
    listen 80; 
    server_name test.ravecy.com; 

    root /var/www/ravecy.com/public; 

    location/{ 
    try_files $uri @ravecy; 
    } 

    location @ravecy { 
    proxy_pass http://ravecy; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection $connection_upgrade; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_redirect off; 
    } 
} 

는 슬프게도, 그러나,이 작동하지 않습니다 워드 프로세서 herehere을 바탕으로 나는 다음의 nginx 설정을 만들었습니다. 나는 이유를 정확하게 모르겠지만 로그에서 본의 nginx는 웹 소켓 연결하지만 일반 HTTP 연결로 내 웹 소켓 연결 시도를 처리하지 않는 것을 나에게 보인다 연결이 닫혀 있는지

==> production.log <== 
Started GET "/chat" for 82.170.121.62 at 2013-04-10 12:20:12 +0200 
Processing by ApplicationController#chat as HTML 
    Rendered application/chat.html.erb within layouts/frontend (0.2ms) 
    Rendered layouts/frontend/_navbar.html.erb (6.3ms) 
    Rendered layouts/shared/_alert.html.erb (0.0ms) 
    Rendered layouts/frontend/_facebook_sdk.html.erb (0.0ms) 
Completed 200 OK in 9ms (Views: 8.4ms | ActiveRecord: 0.4ms) 
Started GET "/websocket" for 82.170.121.62 at 2013-04-10 12:20:12 +0200 

==> websocket_rails.log <== 
I [2013-04-10 12:20:12.744] [ConnectionManager] Connection opened: #<Connnection::47398780> 

I [2013-04-10 12:20:12.745] [Dispatcher] Started Event: client_connected 
I [2013-04-10 12:20:12.745] [Dispatcher] Name: client_connected 
I [2013-04-10 12:20:12.745] [Dispatcher] Data: {"connection_id"=>47398780} 
I [2013-04-10 12:20:12.745] [Dispatcher] Connection: #<Connnection::47398780> 

I [2013-04-10 12:20:12.747] [Dispatcher] Event client_connected Finished in 0.001960819 seconds 


==> /var/log/nginx/access.log <== 
82.170.121.62 - - [10/Apr/2013:12:20:12 +0200] "GET /chat HTTP/1.1" 200 854 "http://test.ravecy.com/posts" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" 
82.170.121.62 - - [10/Apr/2013:12:20:12 +0200] "GET /assets/frontend-6ad91089203a6026624ce015c2800492.css HTTP/1.1" 304 0 "http://test.ravecy.com/chat" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" 
82.170.121.62 - - [10/Apr/2013:12:20:12 +0200] "GET /assets/frontend-98fa493fc9f482c0d44b31bda5a89135.js HTTP/1.1" 304 0 "http://test.ravecy.com/chat" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17" 

==> websocket_rails.log <== 
I [2013-04-10 12:20:12.832] [ConnectionManager] Connection closed: #<Connnection::47398780> 

I [2013-04-10 12:20:12.832] [Dispatcher] Started Event: client_disconnected 
I [2013-04-10 12:20:12.832] [Dispatcher] Name: client_disconnected 
I [2013-04-10 12:20:12.832] [Dispatcher] Data: nil 
I [2013-04-10 12:20:12.832] [Dispatcher] Connection: #<Connnection::47398780> 

I [2013-04-10 12:20:12.833] [Dispatcher] Event client_disconnected Finished in 0.000293462 seconds 


==> /var/log/nginx/access.log <== 
82.170.121.62 - - [10/Apr/2013:12:20:12 +0200] "GET /websocket HTTP/1.1" 200 398 "-" "-" 

주 100ms 이내에 살아 있어야합니다.

또한 CONFIGS : nginx.conf :

user www-data; 
worker_processes 4; 
pid /run/nginx.pid; 

events { 
    worker_connections 768; 
} 

http { 
    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 65; 
    types_hash_max_size 2048; 

    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log; 

    gzip on; 
    gzip_disable "msie6"; 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

얇은 설정 :

--- 
chdir: /var/www/ravecy.com 
environment: production 
address: 127.0.0.1 
port: 3000 
timeout: 30 
log: log/thin.log 
pid: tmp/pids/thin.pid 
max_conns: 1024 
max_persistent_conns: 100 
require: [] 
wait: 30 
servers: 2 
daemonize: true 

의 nginx 버전 : http://test.ravecy.com/chat

예를 살 수 의 nginx/1.3.15 URL 참고 사항 : JS 콘솔에서 new WebSocket("ws://test.ravecy.com/websocket")을 실행하면 브라우저에서 "예기치 않은 응답 코드 : 200"을 표시합니다.

나는이 일을하기에 절박하며, 내가 무엇을해야하는지 더 이상 알지 못한다.

는 텔넷의 nginx 결과 :

텔넷 얇은 직접 결과 :

GET /websocket HTTP/1.1 
Host: test.ravecy.com 
Connection: Upgrade 
Upgrade: WebSocket 

HTTP/1.1 101 Web Socket Protocol Handshake 
Upgrade: WebSocket 
Connection: Upgrade 
WebSocket-Origin: 
WebSocket-Location: ws://test.ravecy.com/websocket 

[["client_connected",{"id":null,"channel":null,"data":{"connection_id":37489460},"success":null,"result":null,"server_token":null}]][["users",{"id":null,"channel":null,"data":[],"success":null,"result":null,"server_token":null}]][["client_connected",{"id":null,"channel":null,"data":{},"success":false,"result":null,"server_token":null}]] 

을 분명히, 이것은 물건을 잘못했다입니다. 하지만 왜?

+0

그것이 HTTP에 고정 된 것 같다 : 연결이 시간에 WS에 대한 HTTP 구문을 업그레이드

그래서 완전한 서버 설정은 다음과 같습니다 대소 문자를 구분 //test.ravecy .com/chat. 그게 뭐야? –

답변

7

텔넷 결과를 토대로 "업그레이드"를 대문자 "U"와 함께 사용했다고 언급했습니다. "업그레이드"대신 "업그레이드"를 사용하여 내가 가지고 있던 모든 문제를 해결했습니다 ...

+0

! 당신은 nginx 설정에서 의미합니까? 'U'는 정확히 어디에서 변경 했습니까? – unludo

+1

'map $ http_upgrade $ connection_upgrade { 기본 업그레이드; ''닫기; }' –

+0

정말 고마워요! 오늘 내 두뇌를 구해 줘. omg 무슨 미친 물건 .. 내 경우에는 올바른 위치 설정이 다음과 같습니다. 'proxy_http_version 1.1;' 'proxy_set_header 업그레이드 $ http_upgrade; ' 'proxy_set_header Connection "Upgrade"; –

0

예.

server { 
listen 80; 
server_name server.com; 

root /var/www/my-app/current/public; 

try_files $uri/index.html $uri @app; 

location @app { 
    proxy_pass http://app; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
} 

# enables WS support 
location /websocket { 
    proxy_pass http://app; 
    proxy_http_version 1.1; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "Upgrade"; 
} 

error_page 500 502 503 504 /500.html; 
client_max_body_size 4G; 
keepalive_timeout 10; 

}

관련 문제