简体   繁体   English

如何在mod_perl的BEGIN块中访问Apache服务器配置?

[英]How can I access the Apache server configuration in a BEGIN block in mod_perl?

I've been trying to switch from using PerlSetEnv to using custom configuration directives . 我一直在尝试从使用PerlSetEnv切换到使用自定义配置指令 I have my configuration module with a copy of set_val from the docs: 我的配置模块带有来自文档的set_val的副本:

sub set_val
{
    local our ($key, $self, $parms, $arg) = @_;
    $self->{$key} = $arg;
    unless ($parms->path)
    {
        local our $srv_cfg = Apache2::Module::get_config($self, $parms->server);
        $srv_cfg->{$key} = $arg;
    }
}

...which is called by every custom directive sub. ...,每个自定义指令子都调用。 Then I have in my .conf: 然后我在.conf中:

PerlLoadModule MyModule::ServerConfig
MyCustomDirective 'hello'

This works fine in that httpd -t okays the file's syntax. 在httpd -t可以使用文件语法的情况下,此方法可以正常工作。 The problem is that I can't seem to get at the value from the config file from within a BEGIN block, which I need to do. 问题是我似乎无法从BEGIN块中获取配置文件中的值,这是我需要做的。

I've tried tinkering with all sorts of things: 我尝试过修改各种事情:

BEGIN
{
    use Apache2::CmdParms ();
#   use Apache2::Directive ();
    use Apache2::Module ();
#   use Apache2::ServerUtil ();
#   use Apache2::RequestUtil ();

    use Data::Dump;
    warn ddx(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::CmdParms->server));
#   warn ddx(Apache2::Directive->as_hash);
#   warn Apache2::ServerUtil->dir_config('MyCustomDirective);
#   warn Apache2::CmdParms->server->server_hostname();
}

...but to no avail. ...但无济于事。 Most of my efforts (trying to access CmdParms->server for instance) result in Parent: child process exited with status 3221225477 -- Restarting and an automatic restart of Apache as it says. 我的大部分工作(例如,尝试访问CmdParms->server )都导致Parent: child process exited with status 3221225477 -- Restarting并按提示自动重启Apache。 If I pass ServerUtil->server to get_config() , the server stays alive but the warning only prints out '1'. 如果我将ServerUtil->server to get_config()传递ServerUtil->server to get_config() ,则服务器保持活动状态,但警告仅打印出“ 1”。

I read somewhere that this is because you can't get at anything request-related within a BEGIN block, because requests vary. 我在某处读到这是因为在BEGIN块中您无法获得任何与请求相关的信息,因为请求各不相同。 It kind of makes sense, except that with PerlOptions +GlobalRequest I have been able to see $ENV within a BEGIN block, so why wouldn't I be able to see my own directives, just as dependent as they are on how the request happens? 这是有道理的,除了使用PerlOptions + GlobalRequest我已经可以在BEGIN块中看到$ENV了,所以为什么我不能看到我自己的指令,就像他们对请求如何发生的依赖一样? Especially confusing is that if I try to pass Apache2::RequestUtil->request->per\\_dir\\_config() to get_config() , it says Global $r object is not available. 尤其令人困惑的是,如果我尝试将Apache2::RequestUtil->request->per\\_dir\\_config()传递给get_config() ,它表示Global $r object is not available. If that's true in a BEGIN block, how is it I can get at $ENV ? 如果在BEGIN块中是正确的,我怎么能得到$ENV

Try add what you want to import function to other module and use this module in code where you usually put BEGIN block. 尝试将要import功能添加到其他模块,并在通常放置BEGIN块的代码中use此模块。 It should work same. 它应该工作相同。 May be it helps. 可能有帮助。

Partly, Dump isn't being used correctly. 在某种程度上,转储使用不正确。 This works better: 这样效果更好:

use Data::Dump qw(pp);
warn pp(Apache2::Module::get_config('MyModule::ServerConfig', Apache2::ServerUtil->server));

However, it doesn't show any directives that appear within <Directory> blocks. 但是,它不显示<Directory>块中显示的任何指令。

In my particular case, though, I don't need that functionality, on second thought; 但是,在我的特定情况下,我不需要考虑这一功能; that just happens to be where I had stuck them. 恰好是我卡住它们的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM