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

Pau*_*cer 30 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
Run Code Online (Sandbox Code Playgroud)

noo*_*odl 34

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

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

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

另外需要注意的是,只有在服务器启动时才会解释这些值,因此它们更像是常量而不是变量.

更新

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

  • 在debian-like安装中,它们在/ etc/apache2/envvars中定义 (2认同)
  • 是的,这就是为什么我说“由你决定”。Debian 的 Apache 包装是一团糟。 (2认同)

Sco*_*eke 7

试试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
Run Code Online (Sandbox Code Playgroud)

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