WebSocket connection fails after updating the application URL path. The application is served through an Apache VirtualHost configuration with Passenger, and while all other application features (API, static assets) are working correctly, the WebSocket connection cannot be established.
Expected Behavior
WebSocket connection should establish successfully for real-time features through the /myapp/cable endpoint.
Current Behavior
WebSocket connection fails to establish. Browser console shows:
WebSocket connection to 'ws://domain/myapp/cable' failed: WebSocket is closed before the connection is established.
All requests to the WebSocket endpoint return āfailedā status in browser Network tab with the following request headers:
Is there a way to gather more detailed information about the source of the error?
How can I properly configure the VirtualHost to handle WebSocket connections in a subdirectory?
Are there common gotchas when setting up Action Cable with Passenger in a subdirectory?
Current VirtualHost configuration:
<VirtualHost *:80>
ServerName xxx
DocumentRoot /var/www/html/mainapp/public
<Directory /var/www/html/mainapp>
Allow from all
Options -MultiViews
Require all granted
</Directory>
<Location /myapp>
PassengerBaseURI /myapp
PassengerAppRoot /var/www/html/myapp
PassengerEnabled on
Require all granted
</Location>
<Directory /var/www/html/myapp/public>
Allow from all
Options -MultiViews
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
It seems that WebSockets have difficulties working with Apache. For now, Iāve decided to give up on setting up WebSockets.
However, Iām facing another issue: I canāt access my app properly. Passenger is running, but when I go to my appās URL, it displays the appās files instead of running the application.
I updated my VirtualHost configuration since this is now a production server with multiple applications already configured:
<VirtualHost *:80>
ServerName xxx
DocumentRoot /var/www/html/public
<Directory /var/www/html/myapp/public>
Allow from all
Options -MultiViews
Require all granted
AllowOverride All
PassengerEnabled on
</Directory>
PassengerBaseURI /myapp
PassengerAppRoot /var/www/html/myapp
</VirtualHost>
And i canāt find any logs from apache or my app giving me detailed informations about this.
Does anyone have an idea why Passenger is not serving the app correctly? Any help would be appreciated!
Your WebSocket issue is likely due to Passenger and Apache not forwarding WebSocket connections properly. Try these steps:
Enable WebSocket support in Apache by adding:
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:3000/$1 [P,L]