35

有没有办法在 apache httpd.conf 文件中使用某种变量?我想定义一个值并在整个块中使用它,如

define myvar somename #or whatever the syntax would be
alias /my/path "/some/path/${myvar} #or whatever the syntax would be
4

2 回答 2

44

是的,有点。您可以在启动时使用${ENVVAR}语法将环境变量替换到配置文件中。在启动服务器之前,由您决定如何设置这些变量。

http://httpd.apache.org/docs/2.2/configuring.html#syntax

请注意,这些变量将持续存在,因此任何使用 php 等语言的脚本都可以读取它们。

同样重要的是要注意这些值仅在服务器启动时才被解释一次,因此它们更像是常量而不是变量。

更新

从 httpd 2.4 版开始,请改为查看此答案:https ://stackoverflow.com/a/15731921/498798

于 2011-01-13T20:28:30.863 回答
8

试试 mod_macro。它实际上允许您使用本质上是变量的内容。模块文档页面中的一个示例给出了它的要点:

## Define a VHost Macro for repetitive configurations

<Macro VHost $host $port $dir>
  Listen $port
  <VirtualHost *:$port>

    ServerName $host
    DocumentRoot $dir

    <Directory $dir>
      # do something here...
    </Directory>

    # limit access to intranet subdir.
    <Directory $dir/intranet>
      Require ip 10.0.0.0/8
    </Directory>
  </VirtualHost>
</Macro>

## Use of VHost with different arguments.

Use VHost www.apache.org 80 /vhosts/apache/htdocs
Use VHost example.org 8080 /vhosts/example/htdocs
Use VHost www.example.fr 1234 /vhosts/example.fr/htdocs

我在http://www.cri.ensmp.fr/~coelho/mod_macro/找到了它的下载地址

于 2013-05-14T05:05:09.533 回答