Access webrick/rails from another computer on local network - ruby-on-rails

I have a rails application running on localhost:3000. I wish to access it from another computer on the same network. I feel like i've done this before with ease, but it's giving me some grief. I can ping the IP of the computer just fine, but hitting ip:3000 in the browser doesnt work. I tried launching rails s -b ipaddress as well, and no luck.
Suggestions?

After making sure your server side firewall is open to the incoming connection on high ports (this is normally true and the default port is 3000, so you probably don't have to do anything) you must also start the server like this:
rails server -b 0.0.0.0
which binds it to the universal address. It binds to localhost by default.
Using this method you don't have to bind to port 80, but you can like this:
rails server -b 0.0.0.0 -p 80
(If you're using rvm then you may need to use rvmsudo)
To make this change more permanent edit your config/boot.rb and add this:
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
Then you should only have to use rails s
Source: https://stackoverflow.com/a/29562898/1795429

rails server -b 0.0.0.0 -p 8000
This worked for me. No firewall issues, and no need to give super user permissions.

Yes, this was a good answer in general:
rails server -b 0.0.0.0
If you use Ubuntu, you probably have to open the port in the firewall:
sudo ufw allow 3000
If your system is running in VirtualBox, you have to check your Network Settings.
In the case of network mode NAT you have to click to the extended options and there to Port Forwarding. Add a rule for TCP protocoll, host port 3000 (or any other), and guest port 3000.

Assuming Webrick starts without issue, this is 100% a firewall issue. You should provide some specifications, like what operating system your host is running and whether or not you have administrator privileges as far as controlling the firewall.
If you're on Linux and running the iptables firewall service, you need to add a rule to accept traffic over port 3000. It would look something like:
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
That command would be a one-time-only solution though, you'd need to extend your current iptables rules script to make it permanent every time your system boots or logs in.
If you're running Windows, depending on whether you're running XP or Vista/7, you'd need to do something similar. I'm going to assume you're in the Vista/7 environment, and you would just need to follow the steps provided through this guide http://windows.microsoft.com/en-US/windows7/Open-a-port-in-Windows-Firewall.

Try running the server on port 80 instead, your firewall is probably blocking port 3000.

I am using foreman to manage my Procfile-based application.
Adding -b 0.0.0.0 to my bundle exec rails s command in Procfile worked for me.

One reason is your ip is not bind to the rails server. You can bind the ip with -b command option.
Usage: rails server [mongrel, thin etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost

Related

How to run local host on Iphone using swift ?

I have a local ruby on rails server set up on -
http://localhost:3000/
I have added allow arbitrary loads in the info.plist
When I run the app on the simulator it works fine, but when I try to run it on the iPhone it doesn't. The app just crashes.
Looking at similar questions I found that I need to use the actual IP. I also turned the Internet sharing on from the preferences.
So I found out my local IP address which is - 192.168.0.8
So in Safari I am testing it out like this http://192.168.0.8:3000/
But it doesn't work!
How can I get this up and running?
Start your rails server like this:
rails server -b 0.0.0.0
Note that if you have a firewall on your computer you may need to open your firewall or run on another port, like port 80.
rails server -b 0.0.0.0 -p 80
It'd be easier if you Ngrok.
Just start your server, as usually and by default with port 3000, and then run ngrok as to start listening on such port:
./ngrok http 3000
This will give you a public IP which will be accessible for your devices.

can't run both phpmyadmin and rails server on cloud9

I am working on a rails project on cloud9 and I followed this tutorial to install PhPmyadmin.
The problem is that I can't run at the same time a rails server and a php my admin server since they both seem to use the same Port.
To launch my rails server, I use rails s -b $IP -p $PORT.
Here is the error I get:
/usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/socket.rb:205:in `bind': Address already in use - bind(2) for 0.0.0.0:8080 (Errno::EADDRINUSE)
Has anyone an idea how to fix this?
phpMyAdmin is just another PHP application, so it doesn't listen on a port. You should instead check your webserver configuration to see what port it is on. Most webservers tend to default to port 80, though, so it might be some other process.
You can use netstat -tulpn | grep 8080 to find what's using that port and modify either your Rails configuration or the other process to use a different port.

What does binding a Rails Server to 0.0.0.0 buy you?

I am using "www.xip.io" as a DNS wildcard for testing on different devices. I set my primary domain to my IP address. I fire up a rails server with bundle exec rails server and I go here www.<ip_address>.xip.io:3000 and notice my rails server doesn't respond.
However, if I bind my rails server to 0.0.0.0 like so bundle exec rails server -b 0.0.0.0, it works! I don't understand what 0.0.0.0 is telling my server for this to work. Can someone make sense of this?
Binding to 0.0.0.0 tells the service to bind to all IP addresses on your machine. Rails server used to do this by default, but with 4.2 changed to binding only to localhost.
Basically if it's only bound to localhost then it will only respond locally to either localhost or 127.0.0.1 which can't work through a DNS service because it's not a public IP address.
When you use 0.0.0.0 it will bind to localhost and to your routable IP address.
I think you need to use binding any time you're in a guest/virtual machine.

No access to a rails server running behind a firewall

I am trying to run a ror application at a friend's home and the application is not reachable from the outside. Here is what we have tried.
The application has been created with
$ rails new <application name>
and started with
$ rails server
The development server starts and is reachable on http://localhost:3000
Next thing we have activated port forwarding on the router he uses to connect to the internet: we have mapped the external port 3000 to port 3000 on the internal host on which the rails server is running. When I try to open the page from the outside (with firefox) I get an error page: unable to conenct to host, the host may be down, try to connect later.
Things we have tried:
Changed port forwarding 80 -> 80
Installed and started Apache: the default Apache page is reachable.
Stopped Apache and started the rails server on port 80 (with sudo, otherwise it can't use port 80): the rails server is not reachable.
I have also checked the firewall of the machine running the rails server. The OS is ubuntu 12.04: the firewall (at least ufw) is not active. I can also connect to that machine using port 22 / ssh.
Summarizing:
rails server is accessible on port 3000 on local host
port forwarding works for ports 22 and 80 and both ssh server and apache server are reachable
rails server is NOT reachable on both port 80 and 3000 from the outside
The only thing I can think of is that rails might have a mechanism similar to Django's ALLOWED_HOSTS and that it refuses external requests because they are for a different host than localhost. However, I haven't found anything on this topic. Also, the application's log files do not show any connection attempt that was refused by the rails server.
So we are out of ideas. What should we check next?
Prior to Rails 4.2, the default for rails server was to bind to all interfaces. In Rails 4.2 this changed to only bind to 127.0.0.1 by default - there's nothing in the application logs because the socket is simply not listening to connection on other network interfaces.
To allow connections from another machine you need to use the -b option to bind to extra ip addresses, eg
rails s -b 0.0.0.0
to bind to all available ip addresses. You can of course replace 0.0.0.0 with one of your machine's actual ip addresses although you would of course have to change that invocation whenever your machine's ip address changed.

Can't connect to Rails server running on EC2 from public IP

I recently have been having a problem with my AWS EC2 instances. The problem is that I cannot hit my Rails servers via public IP, but I can hit localhost and the server will respond.
Here's what I'm doing:
Create new EC2 instance (t2.micro, ubuntu free tier)
Security group has port 22, 80, 3000 open to everyone (0.0.0.0)
SSH to EC2 instance, install rails (I've been using this to install)
Start rails server after install, it's running on port 3000
run "wget localhost:3000" and it returns index.html, yay!
go to my web browser, type in EC2 instance public IP and port 3000 (IP:3000), says it can't connect :(
kill rails server, restart it on port 80, wget works but can't connect via public IP
as a sanity check, I install nginx and run it, and can see the nginx start page on port 80 via public IP... so confused...
So I'm thinking it has something to do with how I'm installing Rails, but I've tried methods other than using that install script but am encountering the same problem... I've even tried creating an entirely new AWS account just in case I screwed up the settings in my original account but haven't had any luck. I have previously been able to get rails running on EC2 instances just fine (in fact I have EC2 instances using the same security group running on my AWS account right now and can hit those public IPs just fine), but am now just banging my head against the wall... any help would be greatly appreciated!
EDIT: For now, I've configured nginx to hit my rails server.... at least that works for now... although I'm still curious why I can't hit my rails server directly...
Check if rails listens on 0.0.0.0 or 127.0.0.1, default is to listen only on localhost.
-b, --binding=IP Binds Rails to the specified IP.
Default: localhost
From the Ruby on Rails 4.2 Release Notes:
Due to a change in Rack, rails server now listens on localhost instead of 0.0.0.0 by default. This should have minimal impact on the standard development workflow as both http://127.0.0.1:3000 and http://localhost:3000 will continue to work as before on your own machine.
If you are using 'rails server' and want to use the default port 3000 then use below:
sudo rails server -b 0.0.0.0
it can be accessed as http://[public_ip]:3000.
If you want to use it as a URL without the port use the below to run the server:
sudo rails server -p 80 -b 0.0.0.0
You just access it as http://[public_ip].
I was running the rails server on default port 3000 on my AWS Ubuntu instance and had allowed this port in the security group, inbound section but still I was unable to access my application by using this.
http://public_ip:3000
I searched a lot and this answer helped me. Explaining the answer. We have to make firewall setting flexible so that our firewall allows port 3000.
You can use this command to check if this port is allowed on firewall or not.
$ sudo iptables -L | grep :3000
If you see nothing it means we have to allow it using this command.
$ sudo iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
This worked for me to get my rails app access on port 3000.

Resources