我在Ubuntu上安装了phusion-passenger和apache。在我的config.ru中,我有以下代码:
require 'cgi'
$tpl = CGI.new['myvar'] + '.rb'
app = proc do |env|
[200, { "Content-Type" => "text/html" }, [$tpl]]
end
run app因此,当我在http://localhost/?myvar=hello上打开我的浏览器时,我看到打印出来的单词是hello,这没问题。然后,我将url更改为http://localhost/?myvar=world,但页面仍然显示hello。只有在我重新加载apache之后,页面才会显示world。
在使用phusion-passenger之前,我使用的是mod_ruby和apache。如果我没记错的话,我不需要重新启动apache来让CGI变量打印更新后的值。
我不需要使用CGI。我只希望能够获取查询字符串参数,而不必每次都重新加载apache。
我没有使用rails或Sinatra,因为我只是试图理解Ruby语言和apache的phusion-passenger到底是什么。
发布于 2014-10-17 03:57:24
IMO这一行为是有意义的。因为$tpl只在文件加载时设置一次,所以在处理第一个请求时会发生什么。在那之后-在下面的请求中-只调用proc,但这不会再更改$tpl。
我不会使用普通的CGI,而是使用一个非常简单的Rack应用程序:
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 Serverhttps://stackoverflow.com/questions/26412341
复制相似问题