【发布时间】:2022-02-26 10:09:50
【问题描述】:
一段时间以来,我一直在努力正确部署我的 Rails 应用程序,并决定是时候向社区寻求帮助了。我已经阅读了几乎所有关于这个问题的 stackoverflow 帖子,包括以下内容,但没有运气:
- Rails 5 ActionCable fails to upgrade to WebSocket on Elastic Beanstalk -> 从这篇文章中我确保我使用的是应用程序负载均衡器
- ActionCable on AWS: Error during WebSocket handshake: Unexpected response code: 404 -> 配置了一个nginx代理,没有变化
问题描述
我正在使用以下设置:
- Ruby 2.7.5
- Rails 6.1.0
- GraphQL
- React 前端(单独的仓库)
- 弹力豆茎
- Ruby 2.7 在 64 位 Amazon Linux 2/3.4.1 上运行
- 应用负载均衡器
- Postgres ActionCable 适配器
我的应用程序已部署到 AWS Elasticbeanstalk,并且对 /graphql 的所有请求均成功。但是,当尝试连接到 /cable 时,我在浏览器控制台中收到此错误:
WebSocket connection to 'wss://api.redacted.io/cable' failed:
检查 Elastic Beanstalk 日志时,我看到:
/var/app/containerfiles/logs/production.log:
I, [2022-02-20T19:35:25.849990 #32761] INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Started GET "/cable" for <redacted IP> at 2022-02-20 19:35:25 +0000
I, [2022-02-20T19:35:25.850342 #32761] INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Started GET "/cable/"[non-WebSocket] for <redacted IP> at 2022-02-20 19:35:25 +0000
E, [2022-02-20T19:35:25.850384 #32761] ERROR -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
I, [2022-02-20T19:35:25.850419 #32761] INFO -- : [8e1d3e86-81cc-4708-89d3-ebad56470f8f] Finished "/cable/"[non-WebSocket] for <redacted IP> at 2022-02-20 19:35:25 +0000
可能相关的文件
cable.yml:
development:
adapter: postgresql
test:
adapter: test
production:
adapter: postgresql
production.rb:
...
config.action_cable.url = 'wss://api.redacted.io/cable'
config.action_cable.allowed_request_origins = ['redacted.io', 'http://redacted.io', 'https://redacted.io']
...
.ebextensions/nginx_proxy.config:
files:
"/etc/nginx/conf.d/websockets.conf" :
content: |
upstream backend {
server unix:///var/run/puma/my_app.sock;
}
server_names_hash_bucket_size 128;
server {
listen 80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server_name redacted.elasticbeanstalk.com;
# prevents 502 bad gateway error
large_client_header_buffers 8 32k;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_pass http://backend;
proxy_redirect off;
location /assets {
root /var/app/current/public;
}
# enables WS support
location /cable {
proxy_pass http://backend/cable;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
container_commands:
01restart_nginx:
command: "nginx -t && service nginx restart"
【问题讨论】:
标签: ruby-on-rails nginx websocket amazon-elastic-beanstalk actioncable