ElasticBeanstalk是一个Application Load Balancer配置。它是一个Ruby on Rails,包含Passenger,Puma和NGINX(默认的Ruby AWS Stack)。
我正在强迫ssl,除了websockets之外,我的一切工作都很完美,似乎是(被阻止|丢弃)。
第一个错误显示在控制台中:( URL是我的域名)
WebSocket connection to 'wss://<URL>/cable' failed: WebSocket is closed before the connection is established.
此请求在前端(OPCODE -1)
失败。
通过检查服务器日志,我显示了access.log
:
/cable"499
这不是一个非常具有描述性的错误。
导轨production.log
显示:
Started GET "/cable/" [WebSocket] for 152.170.14.251 at 2018-05-28 21:01:27 +0000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
Registered connection (Z2lkOi8vYXNlc29yLWFwcC9JZGVudGl0eS81:Z2lkOi8vYXNlc29yLWFwcC9Vc2VyLzI)
WebSocket error occurred: wrong number of arguments (given 2, expected 1)
有时候我看到了另一个错误:
NoMethodError: undefined method `+' for nil:NilClass
File "/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/websocket-driver-0.7.0/lib/websocket/driver/hybi.rb" line 11 in generate_accept
File "/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/websocket-driver-0.7.0/lib/websocket/driver/hybi.rb" line 76 in initialize
File "/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/websocket-driver-0.7.0/lib/websocket/driver.rb" line 160 in new
File "/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/websocket-driver-0.7.0/lib/websocket/driver.rb" line 160 in rack
File "/opt/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/actioncable-5.2.0/lib/action_cable/connection/client_socket.rb" line 47 in initialize
该文件显示缺少密钥。
通过遵循该错误,我发现了这个问题:
这引导我到最后的档案:
File variable that seems to be missing
这最终导致我可能缺少这些配置:
经过大量调查后,我最终得到了宝石源代码,看起来像某些字符的编码失败并创建了一个奇怪的字符串,ActionCable无法解析:
答案 0 :(得分:0)
对于默认的Ruby AWS堆栈(您提到过),您需要编辑默认的nginx配置以支持websockets。 Elastic Beanstalk / Ruby / Puma堆栈的默认nginx配置can be found here.
基本上,您需要为websockets添加这些http标头:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
执行此操作的一种方法是在项目的根目录中添加.ebextensions
文件夹,并使用默认配置的自定义版本放置配置文件。例如:
# .ebextensions/001_nginx.conf
files:
"/opt/elasticbeanstalk/support/conf/webapp_healthd.conf":
owner: root
group: root
mode: "000644"
content: |
upstream my_app {
server unix:///var/run/puma/my_app.sock;
}
log_format healthd '$msec"$uri"'
'$status"$request_time"$upstream_response_time"'
'$http_x_forwarded_for';
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
location / {
proxy_pass http://my_app; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /assets {
alias /var/app/current/public/assets;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /public {
alias /var/app/current/public;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
}
container_commands:
99_restart_nginx:
command: "service nginx restart || service nginx start"
部署此配置后,如果您为EB实例设置了SSH,则可以通过SSH登录并检查您的nginx配置,以查看是否正在使用您的修改版本。如果您未在此处或日志中看到更改且这不是生产实例,则可能需要考虑从AWS控制台重建环境。