Apache Config-具有共享指令的分组虚拟主机?

时间:2019-02-18 08:22:43

标签: apache virtualhost httpd.conf

我用手管理我的apache配置文件,并尝试使其保持整洁,整洁。当我逐站点实施SSL时,越来越多的指令重复使我需要寻找解决方案。有人向我发布了mod_marco,但我的Apache版本不支持它。无论如何,我一直希望有一个更简单的解决方案。这是一对虚拟主机定义,它们与我实际拥有的非常相似。一个用于主站点,一个用于移动站点:

<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    DocumentRoot /webfolder/odometerlog/www
    CustomLog "/logs/example.access.log" combined
    ErrorLog "/logs/example.error.log"
    <Directory "/webfolder/example/www">
        Require all granted
    </Directory>
    Alias "/js/" "/webfolder/example/shared/js/"
    Alias "/css/" "/webfolder/example/shared/css/"
    Alias "/images/" "/webfolder/example/shared/images/"
    Alias "/sounds/" "/webfolder/example/shared/sounds/"
    Alias "/tech/" "/webfolder/example/shared/tech/"
    RewriteEngine On
    RewriteRule ^/archives/([0-9]{4})/([0-9]{2})/$ /archives/index.php?year=$1&month=$2
</VirtualHost>

<VirtualHost *:443>
    ServerName m.example.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    DocumentRoot /webfolder/odometerlog/m
    CustomLog "/logs/example_m.access.log" combined
    ErrorLog "/logs/example_m.error.log"
    <Directory "/webfolder/example/m">
        Require all granted
    </Directory>
    Alias "/js/" "/webfolder/example/shared/js/"
    Alias "/css/" "/webfolder/example/shared/css/"
    Alias "/images/" "/webfolder/example/shared/images/"
    Alias "/sounds/" "/webfolder/example/shared/sounds/"
    Alias "/tech/" "/webfolder/example/shared/tech/"
    RewriteEngine On
    RewriteRule ^/archives/([0-9]{4})/([0-9]{2})/$ /archives/index.php?year=$1&month=$2
</VirtualHost>

这就是我想把它变成什么?我可以将虚拟主机插入一个简单的“组”或“父”容器,然后将其中包含的所有指令共享给其中包含的所有虚拟主机。乍一看,看起来好像我没有保存那么多行配置代码。但是我的许多站点都拥有超过2个单独的虚拟主机。通过使配置更加井井有条,这将使阅读和维护配置变得更加容易。但是,是否存在类似的东西?

<VirtualHostGroup>
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    Alias "/js/" "/webfolder/example/shared/js/"
    Alias "/css/" "/webfolder/example/shared/css/"
    Alias "/images/" "/webfolder/example/shared/images/"
    Alias "/sounds/" "/webfolder/example/shared/sounds/"
    Alias "/tech/" "/webfolder/example/shared/tech/"
    RewriteEngine On
    RewriteRule ^/archives/([0-9]{4})/([0-9]{2})/$ /archives/index.php?year=$1&month=$2

    <VirtualHost *:443>
        ServerName www.example.com
        ServerAlias example.com
        DocumentRoot /webfolder/example/www
        CustomLog "/logs/example.access.log" combined
        ErrorLog "/logs/example.error.log"
        <Directory "/webfolder/example/www">
            Require all granted
        </Directory>
    </VirtualHost>

    <VirtualHost *:443>
        ServerName m.example.com
        DocumentRoot /webfolder/example/m
        CustomLog "/logs/example_m.access.log" combined
        ErrorLog "/logs/example_m.error.log"
        <Directory "/webfolder/example/m">
            Require all granted
        </Directory>
    </VirtualHost>
</VirtualHostGroup>

还要注意,我知道我可以将指令放置在虚拟主机之外,并且它们对于虚拟主机中未明确定义的指令成为“默认”值。但这对我没有帮助,因为我在服务器上托管了许多站点。我正在寻找一种在该设置中添加另一层的方法,可以在“全局”和每个虚拟主机之间定义自己的中间组。

0 个答案:

没有答案