Ruby Sinatra Webservice running on localhost:4567 but not on IP
Asked Answered
A

4

38

I have a ruby(using sinatra) webservice on windows 7 32 bit OS. Its running on port 4567. Its working fine when I use localhost:4567 but when I replace localhost with the local ip of my machine say, 192.168.103.99:4567 it doesn't work, and fails to connect. I have already disabled the firewall, by-pass proxy and added port 4567 to exception, still no luck.

What might be the issue ?

Aculeus answered 30/5, 2013 at 9:28 Comment(9)
Check localhost is bound to IPV4 localhost 127.0.0.1 and not an IPV6 localhost. Also, what web server are you using? Mongrel, Thin, Webrick, Passenger?Stereogram
How to check that ? Its WEBrick 1.3.1Aculeus
ping localhost and see what ip address is resolved. To bind webrick to a specific ip, see this documentation: smyck.net/2007/03/11/how-to-bind-webrick-to-any-ip-addressStereogram
on pinging localhost its returning reply from something like ::1: which I think is ipv4 ?Aculeus
::1 is not ipv4, it's ipv6.Stereogram
Oh, so may be that's the issue, should I bind it to ipv4 ?Aculeus
let us continue this discussion in chatStereogram
sure..waiting for you there. :)Aculeus
Here's a link that helped me once.Simitar
A
60

Following worked for me.

ruby app.rb -o 0.0.0.0

Aculeus answered 2/1, 2014 at 9:37 Comment(0)
A
27

When using the built-in server through the run! if app_file == $0 check, Sinatra's doc explains that set :bind, '0.0.0.0' is required to make the interface available outside the localhost layer.

It is not required to use a custom IP address or a reverse DNS (mydomain.com…): '0.0.0.0' is the legit value expected by Sinatra, which will be interpreted correctly.

Therefore, a minimal, self-contained Sinatra application made available on all interfaces, not only localhost, would be:

require 'sinatra/base'

class MyApp < Sinatra::Base
  set :bind, '0.0.0.0'

  get '/' do
    'Hello World'
  end

  run! if app_file == $0
end
Akene answered 12/4, 2014 at 10:25 Comment(0)
G
11

To set server hostname or IP-address use sinatra setting bind like

set :bind, '192.168.103.99'
Girdle answered 30/5, 2013 at 11:24 Comment(0)
N
1

this

require 'rubygems'
require 'sinatra'
require "dbi"

set :bind, '192.168.200.185'
get '/' do
    'hello word'
end
Nonparticipation answered 11/4, 2014 at 22:51 Comment(1)
This works, but needs a bit of explanation for the answer.Allegro

© 2022 - 2024 — McMap. All rights reserved.