问题
I have phusion-passenger installed with apache on Ubuntu. In my config.ru, I have the following code:
require 'cgi'
$tpl = CGI.new['myvar'] + '.rb'
app = proc do |env|
[200, { "Content-Type" => "text/html" }, [$tpl]]
end
run app
So then when I go to my browser at http://localhost/?myvar=hello
, I see the word hello
printed out, which is fine. Then I change the url to http://localhost/?myvar=world
, but the page still shows hello
. Only after I reload apache will the page show world
.
Before using phusion-passenger, I was using mod_ruby with apache. If I remember correctly, I didn't need to restart apache to get the CGI variable to print the updated value.
I'm not stuck on needing to use CGI. I just want to be able to grab query string parameters without having to reload apache each time.
I'm not using rails or Sinatra because i'm just trying to wrap my head around the Ruby language and what phusion-passenger with apache is all about.
回答1:
IMO this behavior makes sense. Because $tpl
is set only once when the file is loaded, what happens when the first request is served. After that - in following requests - only the proc
is called, but that does not change $tpl
anymore.
Instead of using plain CGI
, I would do it with a very simple Rack
app:
require 'rack'
require 'rack/server'
class Server
def self.call(env)
req = Rack::Request.new(env)
tpl = "#{req.params['myvar']}.rb"
[200, {}, [tpl]]
end
end
run Server
来源:https://stackoverflow.com/questions/26412341/ruby-cgi-needs-to-reload-apache-for-new-value