Can't launch simple Sinatra app using rackup and jRuby (no response from web server)

Andy Jones picture Andy Jones · Jan 12, 2015 · Viewed 11.7k times · Source

I've got a Sinatra "hello world" app that I am trying to run using jRuby. It works when I run the app, but not when I run rackup. Can anyone tell me what is going on here?

Here's the app, in a file 'app.rb':

require 'rubygems'
require 'bundler/setup'
require 'sinatra'

configure do
  set :bind, '0.0.0.0'
end

get '/' do
  'Boo!'
end

I can run this using bundle exec ruby app.rb and it works fine:

jonea@centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec ruby app.rb
[2015-01-12 10:36:06] INFO  WEBrick 1.3.1
[2015-01-12 10:36:06] INFO  ruby 1.9.3 (2014-12-09) [java]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2015-01-12 10:36:06] INFO  WEBrick::HTTPServer#start: pid=31654 port=4567

Here is my config.ru to call the above program:

require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require './app'

run Sinatra::Application

If I run this, it appears to work, but I can't access the server using a web browser:

jonea@centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec rackup -p4567
[2015-01-12 10:29:06] INFO  WEBrick 1.3.1
[2015-01-12 10:29:06] INFO  ruby 1.9.3 (2014-12-09) [java]
[2015-01-12 10:29:06] INFO  WEBrick::HTTPServer#start: pid=31553 port=4567

I note the suspicious lack of "Sinatra has taken the stage..."

Answer

matt picture matt · Jan 12, 2015

When you run the Ruby file directly (or when you add Sinatra.run! to the config.ru file) Sinatra runs its own server. In this case the call to set :bind, '0.0.0.0' will take effect. When you run through rackup this setting is ignored.

The default host that rackup listens to is localhost, so the server will only be available through the same machine, you won’t be able to access it from other machines. To access it through other machines set the --host option:

bundle exec rackup -p4567 --host 0.0.0.0

(Note the output of rackup -h for the current version says the default host is 0.0.0.0, but this is out of date and has been fixed in master.)