Error during WebSocket handshake: Unexpected response code: 502 - ruby-on-rails

I have Rails app on Digital Ocean (Ununtu 18.04 + nginx + passenger + Capistrano). I have chat functionality inside my app based on Action Cable. On my local everything works without problem, but on production i get next error: Error during WebSocket handshake: Unexpected response code: 502.
As i understand something wrong with Action Cable configuration. But everything done as in tutorials so i now really confused and don't know where i should looking for evil. Please help!
production.rb file
config.action_cable.url = '/cable'
config.action_cable.allowed_request_origins = ['http://example.com']
cable.yml file
production:
adapter: redis
url: redis://redis.exmaple.com:6379
messages.coffee file
jQuery(() ->
App.messages = App.cable.subscriptions.create {channel: 'MessagesChannel', id: $('#conversation_id').val() },
received: (data) ->
jQuery('#new_message')[0].reset()
jQuery('#chat').append data.message
)
messages_channel.rb file
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from "conversation_#{params[:id]}"
end
end
/etc/nginx/sites-enabled/myapp file
server {
listen 80;
listen [::]:80;
server_name example.com;
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;
}
}
In nginx i have a lot of such errors:
2019/07/18 07:37:47 [error] 25500#25500: *9088 upstream prematurely closed connection while reading response header from upstream, client: 117.102.40.105, server: example, request: "GET /cable HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.q4FqjUT/agents.s/core:", host: "example.com"
Of course everywhere example.com is replaced with my real domain name.

I have found the problem. It was in redis url in configuration. In each tutorial it write to use next code:
production:
adapter: redis
url: redis://redis.example.com:6379
But looks that it is mistake and correct will be next code:
production:
adapter: redis
url: redis://localhost:6379
So instead you domain should be used localhost. Hope, this will help somebody and will save a lot of time)

Related

ActionCable Rails 5 (Passenger) - failed: Error during WebSocket handshake: Unexpected response code: 404

I am trying to deploy ActionCable and Rails 5 To Production server (DigitalOcean). I have followed all steps mention in the Gorails video here: https://gorails.com/episodes/deploy-actioncable-and-rails-5
The app was deployed using Phusion Passenger + Nginx + Capistrano.
And when I tried to checked my site on production, I got this error on the browser's console:
WebSocket connection to 'ws://139.59.175.34/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
These are my settings:
/etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name my_server_domain;
passenger_enabled on;
rails_env production;
root /home/deploy/my_app_domain/current/public;
# ActionCabel config (disable this if u r not using it)
location /cable {
passenger_app_group_name actioncable_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
production.rb
config.action_cable.url = "/cable"
config.action_cable.allowed_request_origins = ["http://139.59.175.34"]
cable.js
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer("/cable");
}).call(this);
I have tried to change the ActionCable config on config/production.rb to:
config.action_cable.url = [/ws:\/\/*/, /wss:\/\/*/]
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
but still no luck.
I have also looked into the production.log on the server and this is the error that was recorded:
WebSocket error occurred: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0
UPDATE:
Below is firewall setting on the server:
Finally I found the answer to my problem. It turns out that I do not have redis installed on my production server. So I just need to install redis and now everything works great!
To check whether you have redis installed on the server or not, use this command:
redis-cli
If the redis console appear, then you're all set. If an error/warning comes out, you can follow these steps to install and run redis:
sudo apt install redis-server
redis-server
Hope it helps!
Your configuration looks correct.
This issue can happens if you have a firewall running.
Check with your iptables configuration (linux) if something is blocking it (I don't have a specific line of code to give to you though).
If you're on Windows, check that there are no "antivirus" or "firewall" intercepting / blocking the bits. I heard that Node32 is a very good candidate to block websockets.
Update: Can you also try to connect using wss://139.59.175.34/cable instead of ws://139.59.175.34/cable ? (Be sure your NGinx is proxying with SSL). https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/

rails looking into public folder for all URLs in production mode

My rails app isn't working all of a sudden in production mode as every URL is looking into public folder rather than invoking controller actions, resulting errors like
2016/11/16 11:48:23 [error] 25138#0: *9 open() "/var/www/html/looted/public/admin/products" failed (2: No such file or directory), client: 125.99.106.246, server: www.looted.com, request: "GET /admin/products HTTP/1.1", host: "www.looted.com"
When I run it in development, everything works fine. Am running passenger on nginx server with below configuration
server {
listen 80;
server_name looted.com www.looted.com;
root /var/www/html/looted/public;
passenger_enabled on;
client_max_body_size 250M;
passenger_app_env production;
}
I am using Rails 4.2.4, ruby 2.2.1p85, passenger-5.0.29
Can anybody point me the direction to check what's wrong?
Thanks in advance

Nginx shows public directory content instead of executing Rails App

I am trying to execute my Rails Application on Nginx and Passenger but it shows the public directory content instead of executing the application.
server {
server_name 104.236.218.36;
listen 80 default_server;
root /var/www/noise/public;
passenger_enabled on;
rails_spawn_method smart;
rails_env production;
autoindex on;
}
If i remove autoindex on; i am getting the following error.
2015/02/02 06:16:06 [error] 13528#0: *3 directory index of "/var/www/noise/public/" is forbidden, client: 122.178.204.27, server: 104.236.218.36, request: "GET / HTTP/1.1", ho$
The config you show works ok for me, but maybe you have something else set wrong outside the system scope.
You could also specify the Rails app directory try adding:
passenger_app_root /var/www/noise;

Nginx doesn't recognize my Rails 3 application

I have set up nginx + REE + passenger on my Linode VPS, which has been running great for past six month, both for Rails 2.3.x and Sinatra applications.
However this week I tried to add Rails 3 application to the stack, and I keep on getting 404 Not Found. Logs show that nginx doesn't recognize Rails application and is trying to serve it as static.
2010/11/29 23:44:44 [error] 12464#0: *29 "/var/app/modelky/public/index.html"
is not found (2: No such file or directory), client: 90.177.23.122, server:
reedink.com, request: "GET / HTTP/1.1", host: "reedink.com"
2010/11/29 23:44:44 [error] 12464#0: *30 open() "/var/app/modelky/public/favicon.ico"
failed (2: No such file or directory), client: 90.177.23.122,
server: reedink.com, request: "GET /favicon.ico HTTP/1.1", host: "reedink.com"
However, I'm using the same configuration as I use for all my other Rails 2.3.5 and Sinatra applications that works without any problems
server {
listen 80;
server_name www.reedink.com;
rewrite ^(.*) http://reedink.com$1 permanent;
}
server {
listen 80;
server_name reedink.com;
root /var/app/modelky/public;
passenger_enabled on;
}
From what I understand, Rails 3 should be rack compatible, so from the server's point of view, it's no different than any Sinatra application right?
I just built out a rail 3 box on linode this weekend. I started w/ this stackscript
http://www.linode.com/stackscripts/view/?StackScriptID=1288
and then went from there.
here's a copy of my server conf from the nginx.conf
server {
listen 80;
server_name localhost;
root /home/deploy/foo.bar.com/current/public;
passenger_enabled on;
}
i'd also try adding a static index.html file, get nginx working properly and then try and bootstrap the rails app.
Looks like your request is not hitting Rails. I would try to:
put a static index.html in /var/app/modelky/public to see if it shows up
check if the Rails app is in the given path and restart nginx
prestart Passenger on that server and see how it reacts
To prestart Passenger:
http {
server {
listen 80;
server_name www.reedink.com;
rewrite ^(.*) http://reedink.com$1 permanent;
}
server {
listen 80;
server_name reedink.com;
root /var/app/modelky/public;
passenger_enabled on;
}
passenger_pre_start http://reedink.com/;
}

Passenger, Nginx, and Capistrano - Passenger not launching Rails app at all

Essentially, my route is working perfectly, Passenger seems to be loading - all is hunky-dory. Except that nothing Railsy happens. Here's my Nginx log from starting the server to the first request (ignore the different domain/route - it's because I haven't moved the new domain over yet, and it's returning a 403 error because there's no index file in the public folder):
[ pid=24559 file=ext/nginx/HelperServer.cpp:826 time=2009-11-10 00:49:13.227 ]:
Passenger helper server started on PID 24559
[ pid=24559 file=ext/nginx/HelperServer.cpp:831 time=2009-11-10 00:49:13.227 ]:
Password received.
2009/11/10 00:49:53 [error] 24578#0: *1 directory index of "/var/www/***/current/public/" is forbidden, client: 188.221.195.27, server: ***, request: "GET / HTTP/1.1", host: "***"
2009/11/10 00:49:54 [error] 24578#0: *1 open() "/var/www/***/current/public/favicon.ico" failed (2: No such file or directory), client: 188.221.195.27, server: ***, request: "GET /favicon.ico HTTP/1.1", host: "***", referrer: "***"
Someone on the RubyOnRails IRC channel suggested that it might be a webserver permissions problem. I had a suspicion that it might be a filesystem permission problem, but then Nginx runs as www-data and Passenger as root.
I can load static files from within the public directory fine, but no Rails application is being launched. Does anyone have an idea? My head is gradually melting away figuring this one out!
Edit: Here's the vhost file:
server {
listen 80;
server_name ***;
passenger_enabled on;
location / {
root /var/www/***/current/public;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Problem solved, I am a spanner.
I had 'passenger_enabled on;' inside 'location /' not 'server'. I hereby hand in my coding hands.

Resources