是否可以在httpd.conf中使用变量

时间:2011-01-13 20:03:19

标签: apache apache-config

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

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

2 个答案:

答案 0 :(得分:33)

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

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

请注意,这些变量将保持不变,因此像php这样的语言中的任何脚本都能够读取它们。

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

更新

从httpd版本2.4开始,请参阅此答案:https://stackoverflow.com/a/15731921/498798

答案 1 :(得分:7)

尝试mod_macro。它实际上允许您使用本质上是变量的。来自the module's docs page的一个例子给出了它的要点:

## 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/

找到了它的下载
相关问题