铁轨。Websocket连接在连接后直接终止

Rails. Websocket connection terminates directly after connecting

提问人:Seppello 提问时间:8/17/2023 最后编辑:Seppello 更新时间:8/17/2023 访问量:18

问:

我的 Rails 应用程序中的 websocket 连接有问题。当我通过 Postman 连接时,连接终止,没有错误消息。nginx.error.log 或生产日志中没有错误。

我怀疑我的 nginx conf 有问题。

Redis 正在运行

Nginx 会议

upstream puma_smarthome {
server unix:///home/rails/apps/smarthome/shared/tmp/sockets/smarthome-puma.sock;
}

server {
server_name smarthome.this-co.de www.smarthome.this-co.de;

root /home/rails/apps/smarthome/current/public;
access_log /home/rails/apps/smarthome/current/log/nginx.access.log;
error_log /home/rails/apps/smarthome/current/log/nginx.error.log info;

location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

try_files $uri @puma_smarthome;

location @puma_smarthome {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_redirect off;
    proxy_pass http://puma_smarthome;
}

location /cable {
    #proxy_pass http://89.58.12.188:80;
    proxy_pass http://puma_smarthome;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    #proxy_redirect off;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
}

#if ($Scheme = http) {
#     return 301 https://$server_name$request_uri;
#}
error_page 500 502 503 504 /500.html;
client_max_body_size 100M;
keepalive_timeout 10;
listen 80;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/smarthome.this-co.de/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/smarthome.this-co.de/privkey.pem; # managed     by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

生产日志

I, [2023-08-16T21:06:20.550633 #1207931]  INFO -- : [d53bce34-cbb6-4491-abb7-2e76e8af39e1] Started GET "/cable?token=[FILTERED]" for 127.0.0.1 at 2023-08-16      21:06:20 +0200
I, [2023-08-16T21:06:20.555670 #1207931]  INFO -- : [d53bce34-cbb6-4491-abb7-2e76e8af39e1] Started GET "/cable?token=[FILTERED]" [WebSocket] for 127.0.0.1 at 2023-08-16 21:06:20 +0200
I, [2023-08-16T21:06:20.555745 #1207931]  INFO -- : [d53bce34-cbb6-4491-abb7-2e76e8af39e1] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2023-08-16T21:06:20.596028 #1207931]  INFO -- : Registered connection (Z2lkOi8vc21hcnRob21lL1N5c3RlbS80)

邮递员日志

Disconnected from ws://smarthome.this-co.de/cable?token=token
21:06:20
1006 Abnormal Closure:No close frame was received.
{"type":"welcome"}    21:06:20
Connected to ws://smarthome.this-co.de/cable?token=token

彪马.rb

# Puma can serve each request in a thread from an  internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
#bind 'tcp://172.21.12.76:3000'
# Specifies the `worker_timeout` threshold that   Puma will use to wait before
# terminating a worker in development environments.
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"

# Specifies the `port` that Puma will listen on to   receive requests; default is 3000.
workers 2
daemonize true if ENV.fetch("RAILS_ENV",  "development") == "production"
port ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.

environment ENV.fetch("RAILS_ENV") { "production" }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Specifies the number of `workers` to boot in c   lustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart
Ruby-on-Rails Ruby Nginx WebSocket 操作电缆

评论

0赞 dbugger 8/17/2023
“它曾经有效”——发生了什么变化?这是第一个要看的地方。
0赞 dbugger 8/17/2023
这里有一些有趣的读物 stackoverflow.com/questions/19304157/......

答: 暂无答案