Ruby CGI是否需要重新加载Apache以获取新价值? [英] Ruby cgi needs to reload apache for new value?
问题描述
我在Ubuntu上用apache安装了phusion-passenger.在我的config.ru中,我有以下代码:
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
因此,当我通过 http://localhost/?myvar = hello
进入浏览器时,我看到打印出 hello
字样,这很好.然后,将URL更改为 http://localhost/?myvar = world
,但页面仍显示 hello
.只有在重新加载apache之后,页面才会显示 world
.
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
.
在使用Phusion-Passenger之前,我在Apache上使用过mod_ruby.如果我没记错的话,我不需要重新启动apache即可获取CGI变量来打印更新后的值.
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.
我并不需要使用CGI.我只希望能够获取查询字符串参数而不必每次都重新加载apache.
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.
我不使用Rails或Sinatra,因为我只是想把我的头缠在Ruby语言上,而使用Apache的phusion-passenger到底是什么.
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.
推荐答案
IMO,此行为很有意义.因为 $ tpl
在加载文件时仅设置一次,所以在处理第一个请求时会发生什么.之后-在以下请求中-仅调用 proc
,但这不再更改 $ tpl
.
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.
我将使用一个非常简单的 Rack
应用程序来做到这一点,而不是使用普通的 CGI
:
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
这篇关于Ruby CGI是否需要重新加载Apache以获取新价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!