在httpd.conf文件中,使用ServerName作为变量

分享于 

3分钟阅读

互联网

 

问题:

我当前的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>




答案1:

试用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




答案2:

我想,也许可以使用变量声明创建一个配置文件,将其包含在httpd.conf文件中,然后运行cron作业以使用sed将服务器名称更改为当前的服务器名称。

cron作业示例:


set servername = hostname


sed -i -e 's/(?-s)define servername .+/define servername $servername/g' /etc/httpd/conf.d/variables.conf





文件  Server  USE  HTTP  VAR  variable  
相关文章