【问题标题】:Apache module - How to check existence of directive in httpd.confApache 模块 - 如何检查 httpd.conf 中指令的存在
【发布时间】:2015-07-08 08:14:25
【问题描述】:

我正在尝试编写一个示例 Apache 模块来读取配置文件,其文件路径在 httpd.conf 中指定,如下所示:

<Location ~ /(?!.*\.(png|jpeg|jpg|gif|bmp|tif)$)>
        SetInputFilter SAMPLE_INPUT_FILTER
        SetOutputFilter SAMPLE_OUTPUT_FILTER
        ConfigFilePath "/etc/httpd/conf.d/sample/sample.config"
</Location>

在命令记录结构中,我这样做:

static const command_rec config_check_cmds[] =
{
    AP_INIT_TAKE1( "ConfigFilePath", read_config_file, NULL, OR_ALL, "sample config"),
    { NULL }
};

我也设置了:

module AP_MODULE_DECLARE_DATA SAMPLE_module = {
    STANDARD20_MODULE_STUFF, 
    create_dir_config,          /* create per-dir   config structures */
    NULL,                   /* merge per-dir    config structures */
    NULL,                   /* create per-server config structures */
    NULL,                   /* merge per-server config structures */
    config_check_cmds,              /* table of config file commands */
    sample_register_hooks   /* register hooks */
};

我可以成功读取配置文件路径。现在我想检查如果httpd.conf中没有指定“ConfigFilePath”,当我使用“service httpd restart”时它会在控制台显示错误

我该怎么做?

【问题讨论】:

    标签: apache httpd.conf apache-modules


    【解决方案1】:

    您将注册一个ap_hook_post_config 挂钩并在那里验证所需的设置。请注意,该钩子的调用次数是此处答案中记录的两倍:Init modules in apache2

    在此处查看示例后配置挂钩实现:http://svn.apache.org/repos/asf/httpd/httpd/tags/2.3.0/modules/examples/mod_example_hooks.c

    【讨论】:

    • 感谢您的回答,但我仍然不知道如何检查“ConfigFilePath”是否存在。如果此键存在,将调用 read_config_file()。如果我定义了一个全局标志(默认为 true),在 read_config_file() 中我设置为 false。我将在 ap_hook_post_config 中检查这个标志。 Is it right way?
    • 我假设您将ConfigFilePath 的值存储在read_config_file 函数的server/dir 结构中的一个变量中,并将该变量初始化为NULL。因此,您要做的是在 post config 挂钩中检查该变量,如果未设置,则返回/记录错误。
    • 我知道这已经很老了,我现在还在挣扎。在ap_hook_post_config 中验证srv_conf 中的参数集非常简单,因为可以通过ap_get_module_config(s-&gt;module_config, &amp;module) 访问服务器配置。但是目录配置呢?我正在尝试在接受任何请求之前验证每个目录配置。
    猜你喜欢
    • 2016-04-27
    • 2011-03-14
    • 2012-02-05
    • 1970-01-01
    • 2012-02-06
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多