Linux

在 httpd.conf 文件中使用 ServerName 作為變數

  • November 20, 2013

我目前的 httpd.conf 文件看起來像這樣:

<VirtualHost *:443>
   DocumentRoot /var/www/html/www.example1.com
   ServerName www.example1.com
   SSLEngine on
   SSLCertificateFile    /var/www/ssl/www.example1.com/certificate.crt
   SSLCertificateKeyFile /var/www/ssl/www.example1.com/private.key
   SSLCACertificateFile  /var/www/ssl/www.example1.com/bundle.crt
</VirtualHost>

<VirtualHost *:443>
   DocumentRoot /var/www/html/www.example2.com
   ServerName www.example2.com
   SSLEngine on
   SSLCertificateFile    /var/www/ssl/www.example2.com/certificate.crt
   SSLCertificateKeyFile /var/www/ssl/www.example2.com/private.key
   SSLCACertificateFile  /var/www/ssl/www.example2.com/bundle.crt
</VirtualHost>

我希望能夠將“ServerName”儲存在某種類型的變數中,以便能夠簡化文件(類似於這樣):

Define server_name = ServerName

<VirtualHost *:443>
   DocumentRoot /var/www/html/${server_name}
   ServerName ${server_name}
   SSLEngine on
   SSLCertificateFile    /var/www/ssl/${server_name}/certificate.crt
   SSLCertificateKeyFile /var/www/ssl/${server_name}/private.key
   SSLCACertificateFile  /var/www/ssl/${server_name}/bundle.crt
</VirtualHost>

有什麼辦法可以做這樣的事情嗎?我環顧四周,找不到任何有助於解決此問題的解決方案,但我可能採取了錯誤的方法。

嘗試 mod_macro

這是一個例子:

## Define a VHost Macro for repetitive configurations

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

  ServerName $host
  DocumentRoot $dir

  # Public document root
  <Directory $dir>
      Require all granted
  </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

引用自:https://serverfault.com/questions/556169