81

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?

agmcleod
  • 13,321
  • 13
  • 57
  • 96
  • Do you have a firewall blocking this access? – Brian Sep 06 '11 at 20:23
  • Hard to say as it is on my works network, not at home. The computer running it is wired in to the 192.168.100 subnet. My second computer is a laptop on wifi, same subnet. – agmcleod Sep 06 '11 at 20:26
  • 2
    Try running it on port 80 instead of 3000 – VNO Sep 06 '11 at 20:28
  • yep, that would do it. Works now :). If you wish, post your answer and i shall accept, – agmcleod Sep 06 '11 at 20:31
  • I'm having the same issue, though I'm already running rails on port 80 (-p 80) and I still can't access it from other computer on same network. I still can access other apps like SVN and UberSVN web interface from other computers, but not ruby. Any help? – XpiritO Oct 26 '12 at 11:30
  • did you broadcast on its IP as well? Let's say the machine it sits on is `192.168.100.50`, on that machine, run: `rails s -b 192.168.100.50 -p 80` – agmcleod Oct 26 '12 at 13:43
  • @agmcleod, yep, tried that too! I got it to work on port 3000, but I'd appreciate to have it sitting on port 80 – XpiritO Oct 26 '12 at 22:23

7 Answers7

200

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

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
OneHoopyFrood
  • 3,829
  • 3
  • 24
  • 39
  • 5
    Chosen answer didn't help me as firewall was already disabled, however, this one did. – nipponese Mar 29 '15 at 18:49
  • Without disabling the firewall "rails server -b 0.0.0.0" did not work for me. First I had to disable the firewall using: "sudo ufw disable" then ran server using: "rails server -b 0.0.0.0" – Afzal Masood Aug 04 '16 at 08:07
  • 1
    @AfzalMasood, sounds like you pre-configured you ufw to deny all by default. That's a good practice! Just allow port 3000 in your rules and it should play nice. :) – OneHoopyFrood Aug 16 '16 at 22:05
  • @OneHoopyFrood May be but I never changed anything related to firewall or deny all by defualt. – Afzal Masood Aug 17 '16 at 08:22
  • 1
    If you don't have `rvmsudo` you can simply use `sudo rails server -b 0.0.0.0 -p 80` (or just `sudo rails s` if you edit `boot.rb`) if you want to use port 80. – Sam Soffes Mar 14 '17 at 15:41
32
rails server -b 0.0.0.0 -p 8000

This worked for me. No firewall issues, and no need to give super user permissions.

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
tomascharad
  • 3,156
  • 23
  • 24
10
  1. Yes, this was a good answer in general:

    rails server -b 0.0.0.0
    
  2. If you use Ubuntu, you probably have to open the port in the firewall:

    sudo ufw allow 3000
    
  3. 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.

Beauty
  • 865
  • 11
  • 14
4

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.

Lester Peabody
  • 1,868
  • 3
  • 20
  • 42
2

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.

Mosab.Mohamed
  • 111
  • 1
  • 3
2

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

VNO
  • 3,645
  • 1
  • 16
  • 25
0

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
lsc
  • 681
  • 3
  • 8
  • 26