No data received when running "thin start" - ruby-on-rails

I am using ruby 1.9.3 and rvm. I would like to run a thin server with --ssl option. I read in some answers that running "thin start --ssl" should do the trick.
But in my development environment when I run thin start --ssl the terminal runs:
Using rack adapter
Thin web server (v1.5.1 codename Straight Razor)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
And in my web-browser in localhost:3000:
No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
When I run the same command with "rails s thin" it work though, with a different message:
Booting Thin
Rails 3.2.11 application starting in development on http:// 0. 0. 0 .0 :3000
Call with -d to detach
Ctrl-C to shutdown server
I don't know why this happens (maybe because of rvm) but thin is working propely. I wanted to run the first vertion "thin start --ssl" because I couldn't set this ssl option in the "rails s" command.

I found the reason why I had "No data received". If anyone has the same error:
I ran "thin start --ssl" so my server would only respond to https requets. All the http requests were not answered, so that's why there was no data (stupid). By default http requests goes to port 80 while https requests goes to port 443.
"thin start --ssl" works fine for debugging ssl locally and you can test your whole application by forcing ssl in the application controller (so you don't find yourself in the same situation, with no data received).

A much easier (and more real world) solution is to run a web server (like nginx) in front of your rails app. This web server would listen for both http and https traffic, and then just pass everything over to port 3000 to the Rails (thin) server.

Related

Why Puma listen on 'tcp://localhost:3000' instead of 'http://localhost:3000'

I'm a Rails developer. There was a time I want to debug my Rails application runing in local on the mobile. So I tried to update my '/etc/hosts' to add the LAN ip(192.168.0.18) to localhost and make sure your laptop and mobile are connecting to the same LAN.
If I start my Rails application with 'Webwrick', it will listen on 'http://localhost:3000', then everything work well
But if I start my Rails application with 'Puma', it will listen on 'tcp://localhost:3000', and then I can't access the server by my LAN ip and port(192.168.0.18:3000)
The HTTP protocol works on top of TCP, HTTP adds format to the raw TCP, so it kind of means the same thing.
Given that Webrick and Puma are different HTTP servers, they may refer to the same thing in different ways.
I hope this will be the worst of your problems during your journey with Rails! Enjoy!
Try running this command: rails s -b 0.0.0.0 -p 3000

`rails server` using Puma and domain name pointing to 127.0.0.1

I have a Rails application that uses subdomains (legacy application, I've been wanting to change that, not yet). I deployed my app to Heroku and I've started to test Puma because it's the recommended choice for Heroku and the default in the upcoming release of Rails. When I used WEBrick (locally) I was able to test my subdomains using a DNS record that pointed to 127.0.0.1 such as vcap.me, specifically http://vcap.me:3000/ would point to my app and http://abcde.vcap.me:3000/ will correctly set the subdomian to "abcde".
Simply adding gem 'puma' to my Gemfile and runnning bundle, causes rails server to start Puma. Except none of the test domains work: http://localhost:3000/ works, but not http://vcap.me:3000/ or http://lvh.me:3000/
Chrome simply says:
"This webpage is not available
ERR_CONNECTION_REFUSED"
Firefox:
"Unable to connect
Firefox can't establish a connection to the server at vcap.me:3000.
..."
I haven't found a cause/solution, but I suspect it has to do with non HTTP TCP requests supported by Puma, except right know, I'm simply trying a HTTP request through the browser.
Just for the curious, if you haven't heard about vcap.me and similar domains, it's simply a DNS record that points to localhost:
$ dig vcap.me
...
vcap.me. 3048 IN A 127.0.0.1
...
$ dig a.vcap.me
...
a.vcap.me. 3600 IN A 127.0.0.1
...
I feel ashamed, #maxd posted a solution to a very similar question: https://stackoverflow.com/a/28745407/637094 and it works. I still don't understand why I need to bind to vcap.me and I didn't before when I used WEBrick.
rails server -p 3000 -b vcap.me
I'll leave the question open, so maybe someone can expand and we all get a better picture of what's going on
This was issue #782 in the Puma server that was solved on July 18, 2016 here.
Your use of domains like vcap.me was not the issue. That domain be resolved to 127.0.0.1 by the DNS server. The issue was, before it was fixed, that on some systems Puma would by default bind only to the IPv6 resolution of localhost, i.e. ::1. Since vcap.me does not provide a IPv6 resolution, you could not reach Puma by calling http://vcap.me:3000/.
Your observation that rails server -p 3000 -b vcap.me solved the issue is because that is equivalent to rails server -p 3000 -b 127.0.0.1. After that, the server's address matched the IPv4-only name resolution of vcap.me.
Anyway, it's an issue of the past. Now by default, Puma binds to both the IPv4 and IPv6 resolutions of localhost.

How do you set the allowed IP clients for a Rails application

Where do you configure the IP addresses that are permitted to connect to a Rails app?
I have a simple rails application which I have inherited that runs in a development environment on Ubuntu 14.04.
It was working OK until recently when some changes were merged in from git. Now when I run rails s the application appears to start thin as the server, as expected.
=> Booting Thin
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:3000, CTRL+C to stop
netstat shows that rails is in fact listening as expected
tcp 0 0 localhost:3000 *:* LISTEN
I can access the website ok from a browser on the server box using 127.0.0.1:3000 and all appears to work as it should. I can't access it from any other machine as a Connection Refused status is returned because rails is only allowing localhost on port 3000.
If I start Thin from the command line with thin start it returns with a similar setup
>> Using rack adapter
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
But this time Thin is listening for connections from any IP and I can reach the site from another machine. There is a difference in behaviour between starting Thin on its own and starting Thin from rails although both are version 1.5.1. Something in the Rails config is constraining Thin to listen only for connections from localhost.
I have all up to date gems so far as I can tell. I thought the issue might be something to do with eventmachine, but I can't find anything.
Any advice appreciated.
0.0.0.0:3000 is an alias for binding to all interfaces. Localhost is literally only the localhost 127.0.0.1 which is not reachable from outside.
With rails 4 I believe they changed the deafult behavior of rails s so the server no longer listens on external interfaces. You can use
rails s -b 0.0.0.0
This will bind it to 0.0.0.0 (all interfaces),as if you started thin manually.
When you start the server specify the ip on which your rails application will run by binding rails application to specific IP. This can be done with -b option. For example if your ip is 192.168.1.69
rails server -b 192.168.1.69 -p 3000
You need to allow the incoming traffic for the port 3000 when you try to access your app from other devices only with in the same network. check out the documentation for Allowing Incoming Traffic on Specific Ports on ubuntu
Thanks errata
That explains it clearly, and now that I have the keyword "binding", I can see that this question has been asked before. It would be helpful to set the binding in a config file, but it seems this is not possible. I'll try to set an alias for the rails s command and use that.
FYI the change was made in version 4.2 of rails according to the release notes for that version because of a change in the underlying rack library.

Rails + AngularJS + Prerender.io (local server) setup not rendering pages

I am trying to setup Prerender.io server locally. I am Rails + AngularJs app.
Already pulled prerender repo and started the server with node.
$ export PORT=3001
$ node server.js
2015-03-13T08:15:48.152Z starting worker thread #0
2015-03-13T08:15:48.159Z starting worker thread #1
2015-03-13T08:15:48.161Z starting worker thread #2
2015-03-13T08:15:48.163Z starting worker thread #3
2015-03-13T08:15:48.506Z starting phantom on port [12301]
2015-03-13T08:15:48.523Z Server running on port 3001
2015-03-13T08:15:48.524Z starting phantom on port [12304]
2015-03-13T08:15:48.526Z starting phantom on port [12303]
2015-03-13T08:15:48.530Z starting phantom on port [12302]
2015-03-13T08:15:48.541Z Server running on port 3001
2015-03-13T08:15:48.548Z Server running on port 3001
2015-03-13T08:15:48.558Z Server running on port 3001
2015-03-13T08:15:51.750Z started phantom
2015-03-13T08:15:51.755Z started phantom
2015-03-13T08:15:51.757Z started phantom
2015-03-13T08:15:51.758Z started phantom
2015-03-13T08:15:56.715Z getting
If I hit below URL , I am getting page rendered properly .
localhost:3001/http://localhost:3000/register
But when using Rails Prerender middleware , Page is not rendering and its show empty page. I am hitting below URL.
http://localhost:3000/register?_escaped_fragment_=
It seems , its correctly hitting server and response with status 200 , but page is not displayed.
Output with escape fragment
Any help will be much appreciable. I can provide more details if need.
USING PUMA SERVER
Response from Prerender.io
Are you using the Thin webserver? You should make sure you're using a multi process server like Puma or Unicorn since the Rails blocks I/O waiting for the Prerender response.

Using Thin Web Server with HTTP and HTTPS

I'm using the Thin web server to serve my Rails app.
Starting the server with thin start serves http requests.
Starting the server with thin start --ssl serves https requests.
Is there a way to have thin serve both http and https requests concurrently?
The reason I ask is because when I use redirect_to some_path in my controllers, they redirect to http. Since thin is serving https requests, nothing is rendered.
Note: I'm using Rack::SSL in Rails 3.0.7.
(Comment converted to answer as requested.)
Simplest option is probably to run two separate instances of thin: one accepting SSL requests and one accepting plaintext requests. Any reason you don't want to do this? (Alternatively, if thin is running behind another web server, like Apache or Nginx, you only need one instance of thin: the frontend server can report whether the request came in over SSL.)
You can't accept both HTTP and HTTPS connections on the same port. (This is why, by default convention, HTTP runs on port 80 whereas HTTPS runs on port 443.)
you can use foreman (https://github.com/ddollar/foreman);
You create a Procfile with 2 process then start both with forman start command.
put this on a file called Procfile:
web: thin start
ssl: thin start --ssl
Then use foreman start and he start the 2 process.
This is how i am using... hope this helps you!

Resources