There a few posts about this on the web. There isn't much information about setting the SERVER_NAME. Not on rack wiki, or sinatra or webrick or puma. If this is a required header then how to fix would be great.
$ rackup --port 4000
[2015-04-15 10:21:31] INFO WEBrick 1.3.1
[2015-04-15 10:21:31] INFO ruby 1.9.3 (2015-01-29) [java]
[2015-04-15 10:21:31] INFO WEBrick::HTTPServer#start: pid=21255 port=4000
^[[1;5ARack::Lint::LintError: env missing required key SERVER_NAME
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/lint.rb:20:in `assert'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/lint.rb:259:in `check_env'
org/jruby/RubyArray.java:1613:in `each'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/lint.rb:255:in `check_env'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/lint.rb:43:in `_call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/lint.rb:37:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/showexceptions.rb:24:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/commonlogger.rb:33:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/sinatra-1.4.6/lib/sinatra/base.rb:218:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/chunked.rb:54:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/content_length.rb:15:in `call'
/usr/local/rvm/gems/jruby-1.7.19/gems/rack-1.6.0/lib/rack/handler/webrick.rb:89:in `service'
/usr/local/rvm/rubies/jruby-1.7.19/lib/ruby/1.9/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/jruby-1.7.19/lib/ruby/1.9/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/jruby-1.7.19/lib/ruby/1.9/webrick/server.rb:191:in `start_thread'
127.0.0.1 - - [15/Apr/2015:10:22:04 -0700] "GET /somepath HTTP/1.1" 500 44080 0.4410
However this is only part of the issue. This is of course in development mode since I have not put any options on my rackup command, but here take a look at what happens when I do add these parameters. This is also using puma this time since I am going crazy trying to figure this out
I edited this a bit just to not show anything company related, just paths and such.
rackup --port 4000 --env production
Puma 2.11.2 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://localhost:4000
I, [2015-04-15T13:30:09.559000 #22412] INFO -- : auth_token ####DELETED####
I, [2015-04-15T13:30:10.290000 #22412] INFO -- : got token ####DELETED####
127.0.0.1 - - [15/Apr/2015:13:30:10 -0700] "GET /somepath HTTP/1.0" 200 196 0.8590
2015-04-15 13:30:10 -0700: Read error: #<NoMethodError: undefined method `to_java_bytes' for :SERVER_NAME:Symbol>
puma-2.11.2-java/lib/puma/java_io_buffer.rb:23:in `<<'
puma-2.11.2-java/lib/puma/java_io_buffer.rb:28:in `append'
org/jruby/RubyArray.java:1613:in `each'
puma-2.11.2-java/lib/puma/java_io_buffer.rb:28:in `append'
puma-2.11.2-java/lib/puma/server.rb:591:in `handle_request'
org/jruby/RubyArray.java:1613:in `each'
puma-2.11.2-java/lib/puma/server.rb:590:in `handle_request'
rack-1.6.0/lib/rack/utils.rb:482:in `each'
org/jruby/RubyHash.java:1341:in `each'
rack-1.6.0/lib/rack/utils.rb:481:in `each'
puma-2.11.2-java/lib/puma/server.rb:576:in `handle_request'
puma-2.11.2-java/lib/puma/server.rb:375:in `process_client'
puma-2.11.2-java/lib/puma/server.rb:262:in `run'
org/jruby/RubyProc.java:271:in `call'
puma-2.11.2-java/lib/puma/thread_pool.rb:104:in `spawn_thread'
Here the environment says production and it is using the database.yml production settings, but the lint is still happening. The final solution is to use RACK_ENV=production rackup --port 4000 which will not show these lint errors or throw a 500.
This is very odd because https://github.com/rack/rack/blob/master/lib/rack/server.rb#L334 sets the ENV['RACK_ENV'] to the option that you pass to the rackup command! Am I missing something here? What is going on? Just wanted to save some people the headache I had with this.
I am having this same problem with rack-1.5.2. rack-1.6.1 does not seem to have this problem.
@earlonrails Can you please provide some extra details? Which app are you trying to start? If closed source, would you mind to share some snippet to reproduce it? Which version of JRuby are you using? Thank you in advance.
@jodosha I will try to make a repo that replicates this error, I think this was on Ubuntu using jruby 1.7.19, not sure if I was getting this error locally. I am not getting it right now with the most recent version of rack. Maybe this is fixed. Leave open for me a few more days and I will post update.
@earlonrails Thank you very much. We're here to help. :smile:
I haven't had time to replicated the issue and was able to work around it. Just closing ticket, thnx for help.
Am encountering running under Docker. Now sure if I should open a new issue, but the gist of it is:
rackup --host 0.0.0.0. Let's call the container sinatra. This was launched using docker run -p 9292:9292 --name sinatra ...rails) started in another docker container, linked to the above using docker run --link sinatra ...From the host (OS X), I can curl localhost:9292 succesfully.
From the rails container, if I try curl sinatra:9292 I get the Rack::Lint::LintError: env missing required key SERVER_NAME.
Is it possible to reopen this issue? I'm seeing the exact same behavior that @aisrael described above. I can only reproduce via that same Docker setup. Here's an example:
Server launched via:
bundle exec rackup -p 9292 -o 0.0.0.0 -E $RACK_ENV
Calling container:
root@9a7cf1a99402:/# env
HOSTNAME=9a7cf1a99402
TERM=xterm
BRAIN_HOST=swarm_brain:9292
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
no_proxy=*.local, 169.254/16
_=/usr/bin/env
root@9a7cf1a99402:/# curl --verbose --request POST --data "" http://$BRAIN_HOST/status
* Hostname was NOT found in DNS cache
* Trying 172.19.0.2...
* Connected to swarm_brain (172.19.0.2) port 9292 (#0)
> POST /status HTTP/1.1
> User-Agent: curl/7.38.0
> Host: swarm_brain:9292
> Accept: */*
> Content-Length: 0
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 500 Internal Server Error
< Content-Type: text/html; charset=ISO-8859-1
* Server WEBrick/1.3.1 (Ruby/2.4.1/2017-03-22) is not blacklisted
< Server: WEBrick/1.3.1 (Ruby/2.4.1/2017-03-22)
< Date: Thu, 15 Jun 2017 10:45:49 GMT
< Content-Length: 351
< Connection: close
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD><TITLE>Internal Server Error</TITLE></HEAD>
<BODY>
<H1>Internal Server Error</H1>
undefined method `bad_request?' for #<Rack::ShowExceptions:0x00000002baca50>
<HR>
<ADDRESS>
WEBrick/1.3.1 (Ruby/2.4.1/2017-03-22) at
:80
</ADDRESS>
</BODY>
</HTML>
* Closing connection 0
root@9a7cf1a99402:/#
Server container:
swarm_brain_1 | Rack::Lint::LintError: env missing required key SERVER_NAME
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:20:in `assert'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:259:in `block in check_env'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:258:in `each'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:258:in `check_env'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:43:in `_call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/lint.rb:37:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/common_logger.rb:33:in `call'
swarm_brain_1 | /bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:231:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/chunked.rb:54:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/content_length.rb:15:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/handler/webrick.rb:86:in `service'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread'
swarm_brain_1 | [2017-06-15 10:46:52] ERROR NoMethodError: undefined method `bad_request?' for #<Rack::ShowExceptions:0x00000002baca50>
swarm_brain_1 | (erb):145:in `pretty'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/erb.rb:896:in `eval'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/erb.rb:896:in `result'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/show_exceptions.rb:96:in `pretty'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/show_exceptions.rb:32:in `rescue in call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/show_exceptions.rb:23:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/common_logger.rb:33:in `call'
swarm_brain_1 | /bundle/gems/sinatra-2.0.0/lib/sinatra/base.rb:231:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/chunked.rb:54:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/content_length.rb:15:in `call'
swarm_brain_1 | /bundle/gems/rack-2.0.3/lib/rack/handler/webrick.rb:86:in `service'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
swarm_brain_1 | /root/.rbenv/versions/2.4.1/lib/ruby/2.4.0/webrick/server.rb:290:in `block in start_thread'
Port 9292 of the server container is mapped to port 5000 of the host.
This is interesting. When I run with -E production (to get past the linter), and spit out the environment in the request, I see:
swarm_brain_1 | {"CONTENT_TYPE"=>"application/x-www-form-urlencoded", "GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/api/heartbeat", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"172.19.0.3", "REMOTE_HOST"=>"172.19.0.3", "REQUEST_METHOD"=>"POST", "REQUEST_URI"=>"http:/api/heartbeat", "SCRIPT_NAME"=>"", "SERVER_PORT"=>"80", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.4.1/2017-03-22)", "HTTP_USER_AGENT"=>"curl/7.38.0", "HTTP_HOST"=>"swarm_brain:9292", "HTTP_ACCEPT"=>"*/*", "rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x00000002b15448>, "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "rack.url_scheme"=>"http", "rack.hijack?"=>true, "rack.hijack"=>#<Proc:0x00000002b14868@/bundle/gems/rack-2.0.3/lib/rack/handler/webrick.rb:74 (lambda)>, "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/api/heartbeat", "sinatra.commonlogger"=>true, "rack.logger"=>#<Logger:0x00000002b140c0 @level=1, @progname=nil, @default_formatter=#<Logger::Formatter:0x00000002b14048 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00000002b174f0 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00000002b17810>>>, "rack.request.query_string"=>"", "rack.request.query_hash"=>{}, "rack.tempfiles"=>[], "rack.request.form_hash"=>{}, "rack.request.form_vars"=>"", "rack.request.form_input"=>#<StringIO:0x00000002b15448>, "sinatra.route"=>"POST /api/heartbeat"}
SERVER_NAME and SERVER_PORT are getting stripped. This doesn't happen when I make the request from the host:
swarm_brain_1 | {"CONTENT_TYPE"=>"application/x-www-form-urlencoded", "GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/api/heartbeat", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"172.19.0.1", "REMOTE_HOST"=>"172.19.0.1", "REQUEST_METHOD"=>"POST", "REQUEST_URI"=>"http://localhost:5000/api/heartbeat", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"localhost", "SERVER_PORT"=>"5000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.4.1/2017-03-22)", "HTTP_HOST"=>"localhost:5000", "HTTP_USER_AGENT"=>"curl/7.51.0", "HTTP_ACCEPT"=>"*/*", "rack.version"=>[1, 3], "rack.input"=>#<StringIO:0x00000002e84520>, "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "rack.url_scheme"=>"http", "rack.hijack?"=>true, "rack.hijack"=>#<Proc:0x00000002e843b8@/bundle/gems/rack-2.0.3/lib/rack/handler/webrick.rb:74 (lambda)>, "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/api/heartbeat", "sinatra.commonlogger"=>true, "rack.logger"=>#<Logger:0x00000002e841d8 @level=1, @progname=nil, @default_formatter=#<Logger::Formatter:0x00000002e84160 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00000002e84110 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x00000002e84070>>>, "rack.request.query_string"=>"", "rack.request.query_hash"=>{}, "rack.tempfiles"=>[], "rack.request.form_hash"=>{}, "rack.request.form_vars"=>"", "rack.request.form_input"=>#<StringIO:0x00000002e84520>, "sinatra.route"=>"POST /api/heartbeat"}
I need to get to work, but I'll try to dig deeper tonight.
After further research I believe this is an issue with WEBrick. I noticed that SERVER_NAME and SERVER_PORT were nil very early in the stacktrace, and subsequently confirmed that this problem does not occur when running via Puma (rackup -s puma).
seeing the same with a simple nginx pointing to a rackup app with webrick :/
FYI not using rackup works ...
require 'rack'
require 'rack/server'
Rack::Server.start(app: ->(_) { [200, {}, ['Hello World']] })
i've fixed it by replacing host header on nginx
location /service/ {
proxy_set_header Host 127.0.0.1;
proxy_pass http://service/;
}
Add RACK_ENV=production right before your rackup command, it should work.
RACK_ENV=production bundle exec rackup -p 3000 -o 0.0.0.0
Hope this would be helpful!
Most helpful comment
Is it possible to reopen this issue? I'm seeing the exact same behavior that @aisrael described above. I can only reproduce via that same Docker setup. Here's an example:
Server launched via:
bundle exec rackup -p 9292 -o 0.0.0.0 -E $RACK_ENVCalling container:
Server container:
Port 9292 of the server container is mapped to port 5000 of the host.