Rails6 + Nginx + Passenger + ActionCable = websocket 关闭
Rails6 + Nginx + Passenger + ActionCable = websocket is closed
Rails 5 有一个类似但未回答的问题,但是,因为我正在使用 Rails 6,所以我再次提问。
我在生产环境中使用动作电缆时遇到了一些问题。 开发环境运行良好。
要解决的错误:WebSocket connection to 'wss://myapp.com/cable' failed: WebSocket is closed before the connection is established.
我在 Chrome 的控制台上多次收到此错误。
堆栈
- Ruby2.6.5
- Rails6.0.3.2
- Nginx 1.14.0
nginx.conf
server {
listen 443 ssl;
server_name *.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem;
root /home/deploy/myapp/current/public;
passenger_enabled on;
passenger_app_env production;
location /cable {
passenger_app_group_name myapp_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size
client_max_body_size 100m;
location ~ ^/(assets|packs) {
expires max;
gzip_static on;
}
}
cable.yml
development:
adapter: redis
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: myapp_production
production.rb
config.action_cable.url = 'wss://app.myapp.com/cable'
config.action_cable.allowed_request_origins = [ 'https://app.myapp.com' ]
这些行在日志中不断重复:
log/production.log
I, [2020-09-15T13:05:02.679580 #32158] INFO -- : Finished "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:02 +0000
I, [2020-09-15T13:05:03.791781 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable" for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792248 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792320 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPG
浏览器控制台
WebSocket connection to 'wss://app.myapp.com/cable' failed: WebSocket is closed before the connection is established.
能否在 production.rb
中评论这一行
config.action_cable.url = 'wss://app.myapp.com/cable'
此外,请确保在您的视图布局文件中包含此行默认名称为 application.html.erb。
在头部添加这个。
<%= action_cable_meta_tag %>
Documentation 表示:
8.3 Consumer Configuration
To configure the URL, add a call to action_cable_meta_tag in your HTML layout HEAD. This uses a URL or path typically set via config.action_cable.url in the environment configuration files.
为什么这在生产中需要而不是开发中没有解释或不清楚。
Rails 5 有一个类似但未回答的问题,但是,因为我正在使用 Rails 6,所以我再次提问。
我在生产环境中使用动作电缆时遇到了一些问题。 开发环境运行良好。
要解决的错误:WebSocket connection to 'wss://myapp.com/cable' failed: WebSocket is closed before the connection is established.
我在 Chrome 的控制台上多次收到此错误。
堆栈
- Ruby2.6.5
- Rails6.0.3.2
- Nginx 1.14.0
nginx.conf
server {
listen 443 ssl;
server_name *.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem;
root /home/deploy/myapp/current/public;
passenger_enabled on;
passenger_app_env production;
location /cable {
passenger_app_group_name myapp_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size
client_max_body_size 100m;
location ~ ^/(assets|packs) {
expires max;
gzip_static on;
}
}
cable.yml
development:
adapter: redis
test:
adapter: test
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: myapp_production
production.rb
config.action_cable.url = 'wss://app.myapp.com/cable'
config.action_cable.allowed_request_origins = [ 'https://app.myapp.com' ]
这些行在日志中不断重复: log/production.log
I, [2020-09-15T13:05:02.679580 #32158] INFO -- : Finished "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:02 +0000
I, [2020-09-15T13:05:03.791781 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable" for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792248 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Started GET "/cable/" [WebSocket] for xxx.xxx.133.88 at 2020-09-15 13:05:03 +0000
I, [2020-09-15T13:05:03.792320 #32158] INFO -- : [2c7c3ddf-e0d9-4f6b-80d5-ace193462c8a] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPG
浏览器控制台
WebSocket connection to 'wss://app.myapp.com/cable' failed: WebSocket is closed before the connection is established.
能否在 production.rb
中评论这一行config.action_cable.url = 'wss://app.myapp.com/cable'
此外,请确保在您的视图布局文件中包含此行默认名称为 application.html.erb。
在头部添加这个。
<%= action_cable_meta_tag %>
Documentation 表示:
8.3 Consumer Configuration
To configure the URL, add a call to action_cable_meta_tag in your HTML layout HEAD. This uses a URL or path typically set via config.action_cable.url in the environment configuration files.
为什么这在生产中需要而不是开发中没有解释或不清楚。