【发布时间】:2021-08-14 05:02:28
【问题描述】:
我无法将 ActionCable 连接到我的 prod 环境中,并且相关问题还没有有效的解决方案。我在 Ubuntu 20.04 上使用带有 Rails 6.1.3.2 的 nginx+puma 设置。我已经确认 redis-server 正在端口 6379 上运行,并且 Rails 正在作为生产运行。
这是我在日志中得到的内容:
I, [2021-05-25T22:47:25.335711 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Started GET "/cable" for 74.111.15.223 at 2021-05-25 22:47:25 +0000
I, [2021-05-25T22:47:25.336283 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Started GET "/cable/"[non-WebSocket] for 74.111.15.223 at 2021-05-25 22:47:25 +0000
E, [2021-05-25T22:47:25.336344 #72559] ERROR -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
I, [2021-05-25T22:47:25.336377 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Finished "/cable/"[non-WebSocket] for 74.111.15.223 at 2021-05-25 22:47:25 +0000
这种情况每隔几秒就会发生一次。您可以在浏览器控制台中看到匹配的输出:
首先,我很确定我需要在我的 nginx 站点配置中添加一些部分,例如 /cable 部分,但我还没有找到正确的设置。这是我当前的配置:
server {
root /home/rails/myapp/current/public;
server_name myapp.com;
index index.htm index.html;
location ~ /.well-known {
allow all;
}
location / {
proxy_pass http://localhost:3000;
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_set_header X-Forwarded-Proto $scheme;
}
# needed to allow serving of assets and other public files
location ~ ^/(assets|packs|graphs)/ {
gzip_static on;
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myapp.com/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
}
server {
if ($host = myapp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myapp.com;
return 404; # managed by Certbot
}
这是我的config/cable.yml:
development:
adapter: async
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: myapp_production
在config/environments/production.rb 中,我将这些行注释掉了:
# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
# config.action_cable.url = 'wss://example.com/cable'
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
我没有在我的路线或任何东西中手动安装 ActionCable。这是您可以获得的标准设置。我认为答案集中在正确的 nginx 配置中,但我不知道它应该是什么。不过,也许也需要 Rails 配置设置。我不记得与乘客一起部署时必须更改任何内容,但也许 puma 是另一回事。
更新
我还注意到,其他问题like this one 中提出的许多解决方案似乎都引用了tmp/sockets/ 中的.sock 文件。不过,我的 sockets/ 目录是空的。除了 ActionCable,Web 服务器运行良好。
更新 #2
我还注意到,即使在重新启动 Rails 后,将 config.action_cable.url 更改为 ws://myapp.com 而不是 wss://myapp.com 也没有任何效果。浏览器控制台错误仍然说它试图连接到wss://myapp.com。可能是由于我如何设置强制将 HTTP 重定向到 HTTPS。不知道跟这个有没有关系?
【问题讨论】:
-
我看到它返回 404,所以原因可能与配置
config.action_cable.allowed_request_origins和config.action_cable.allow_same_origin_as_host有关 -
@LamPhan 我想可能是这样,但是将它们分别设置为我的应用程序 URL 和
true似乎没有任何效果,即使在重新启动 Rails 和 nginx 之后也是如此。 -
我想你错过了
location /cable { ... },你可以试试我的生产配置location /cable { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade "websocket"; proxy_set_header Connection "Upgrade"; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; },在你的情况下用http://localhost:3000替换http://backend -
@LamPhan 我添加了那个配置,nginx 重启成功。没有解决问题。在我的浏览器中访问
/cable告诉我将config.hosts << "localhost"添加到我的Rails prod 配置中,我这样做了。现在访问页面说“找不到页面”。对/cable的请求仍然是 404ing。我是否可能需要在该位置手动启动 ActionCable? -
在我的产品配置中,我设置了
upstream backend并设置了代理location \cable,但我没有看到你的配置,你可以检查一下:stackoverflow.com/questions/55702029/…
标签: ruby-on-rails nginx redis puma actioncable