macros,模板,函数或类似的Apache的conf文件

在刚刚解决的另一个问题之后,我想知道:在Apache中我们可以做些什么来避免重复configuration行? 外面的apache呢? 在我的情况下,这将是AWS EC2的RHEL4,但我相信任何** nix *都会有类似的解决scheme。 也许像sed ,也许使用.htaccess …不知道。 但是,这应该是预处理的,在运行时什么也不做:就像conf文件一样,加载apache就是这样。


在这里,我将复制我以前的解决scheme来说明:

 UseCanonicalNames off NameVirtualHost *:8888 <VirtualHost *:8888> ServerName example1.com ServerAlias *.example1.com # below, stuff that will be repeated # redirect to HTTPS RewriteEngine on RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^login\.(.*)$ RewriteRule ^(.*) https://%1/login$1 [L] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$ RewriteRule ^/login(.*) https://%1/login$1 [L] </VirtualHost> <VirtualHost *:8888> ServerAlias * VirtualDocumentRoot /var/www/html/%-3 # below, stuff that need to be repeated # redirect to HTTPS RewriteEngine on RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^login\.(.*)$ RewriteRule ^(.*) https://%1/login$1 [L] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$ RewriteRule ^/login(.*) https://%1/login$1 [L] </VirtualHost> 

而且我确定有很多情况下,像redirection_to_HTTPS这样的东西可能是一个函数/过程/macros/模板/包含 /等

那么,感谢所有这些伟大的答案,但…可能mod_macro将是最好的伎俩。

其理想与我期待的好处一致:

  • 更小的configuration文件来维护。
  • 由于复制粘贴只是部分更新而减less了错误。
  • 更好的可读性。 例如:使用AllowLocalAccess可能被认为比allow from 192.54.172.0/24 192.54.148.0/24 10.0.0.0/8
  • 所有这些都不以perlm4编程的价格。

下一次,我有机会尝试所有的解决scheme,从这个开始,并添加一个我用过的模板 …除非有人善意地先做所有的事情! 🙂

我可能被称为“老派”只是为了张贴这个,但我认为这个问题的经典解决scheme是使用一个像macros,这是一个POSIX标准的处理器。 有一个GNU实现:

http://www.gnu.org/software/m4/

该页面提到autoconf是其最显着的用户,但我想大多数系统pipe理员首次通过经典的sendmail进入M4。

例如,你可以保留你的configuration文件的M4版本和一些Makefiles,然后通过一个简单的make它们“编译”成实际的运行configuration。

Apache有一个包含指令 。 就像是

 UseCanonicalNames off NameVirtualHost *:8888 <VirtualHost *:8888> ServerName example1.com ServerAlias *.example1.com Include /etc/apache2/redirect_to_https.conf </VirtualHost> <VirtualHost *:8888> ServerAlias * VirtualDocumentRoot /var/www/html/%-3 Include /etc/apache2/redirect_to_https.conf </VirtualHost> 

使用/etc/apache2/redirect_to_https.conf

  # below, stuff that will be repeated # redirect to HTTPS RewriteEngine on RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^login\.(.*)$ RewriteRule ^(.*) https://%1/login$1 [L] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$ RewriteRule ^/login(.*) https://%1/login$1 [L] 

尽pipe如此,其他答案对于将代码转换成configuration文件的更普遍的问题也是有益的。

像这样的事情可以用像Puppet或Capistrano这样的pipe理框架来完成。 在这些脚本中创build基于定义的条件生成httpd.conf文件(和相关的包含文件)。 这种例行的“做这一件事X地方”的行为可以是function化的。 我不相信做这种事情是有现成的方法的,但是这些框架是为了让你自己做的而devise的。