gpt4 book ai didi

ruby-on-rails - Thin + Nginx + Websockets 配置 | rails

转载 作者:行者123 更新时间:2023-12-04 11:54:31 24 4
gpt4 key购买 nike

我有一个带有 websocket-rails gem 的 Ruby on Rails 3.2 应用程序,它在 nginx 反向代理后面的瘦网络服务器中运行。

除了 nginx 反向代理,一切正常。通过删除 nginx 反向代理,websocket 通信工作正常。 (开发和生产)。使用 nginx 作为 websockets 的反向代理是问题开始的地方。

Nginx 版本 1.3.13 并且 up 应该能够支持 websocket 代理。基于文档 herehere我创建了以下 nginx 配置:

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;
}
}

然而,遗憾的是,这行不通。我不完全知道为什么,但在我看来,nginx 没有将我的 websocket 连接尝试作为 websocket 连接处理,而是作为常规 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 "-" "-"

请注意,连接在 100 毫秒内关闭,即使它应该保持事件状态。

更多配置:
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版本:
nginx/1.3.15
网址以现场示例:
http://test.ravecy.com/chat

另请注意:在执行 new WebSocket("ws://test.ravecy.com/websocket") 时在 JS 控制台中,
浏览器提示“意外响应代码:200”。

我非常渴望完成这项工作,并且不再知道我应该做什么。

Telnet 结果 NGINX:
GET /websocket HTTP/1.1
Host: test.ravecy.com
Connection: Upgrade
Upgrade: WebSocket

HTTP/1.1 200 OK
Server: nginx/1.3.15
Date: Sat, 13 Apr 2013 19:50:35 GMT
Content-Type: text/json
Transfer-Encoding: chunked
Connection: keep-alive

152
[["client_connected",{"id":null,"channel":null,"data":{"connection_id":37160040},"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}]]

Telnet 结果直接到thin:
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}]]

显然,这是出了问题。但为什么?

最佳答案

根据 telnet 结果,我注意到我使用了带有大写字母“U”的“升级”。使用“升级”而不是“升级”解决了我遇到的所有问题......

关于ruby-on-rails - Thin + Nginx + Websockets 配置 | rails ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com