apache 我是否必须为端口 80 和 443 复制 Virtualhost 指令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:24:55  来源:igfitidea点击:

Do I have to duplicate the Virtualhost directives for port 80 and 443?

apachevirtualhost

提问by scotts

I have a long and intricate list of <VirtualHost> directives, and I have to duplicate them into separate <VirtualHost> groups for ports 80 and 443 because I'm using SSL. Whenever I update my mod_rewrite rules I have to remember to do it in both places or else I'll break my app... this duplication is asking for trouble. Is there a way to combine or alias these -- the only difference between the two is that the port 443 version contains the SSLEngine, SSLCertificateFile and the like.

我有一个长而复杂的 <VirtualHost> 指令列表,我必须将它们复制到端口 80 和 443 的单独 <VirtualHost> 组中,因为我使用的是 SSL。每当我更新我的 mod_rewrite 规则时,我必须记住在两个地方都这样做,否则我会破坏我的应用程序......这种重复是自找麻烦。有没有办法组合或别名这些 - 两者之间的唯一区别是端口 443 版本包含 SSLEngine、SSLCertificateFile 等。

My <Virtualhost> contains many mod_rewrite rules, LocationMatch rules, CGI directives, etc.

我的 <Virtualhost> 包含许多 mod_rewrite 规则、LocationMatch 规则、CGI 指令等。

Also, I can't use .htaccess files.

另外,我不能使用 .htaccess 文件。

采纳答案by sfossen

Can't you use an include directive to include the common rules. here

你不能使用包含指令来包含通用规则。这里

article

文章

eg.:

例如。:

<VirtualHost _default_:80>
    ...
    include conf/common_rule.conf
</VirtualHost>

<VirtualHost _default_:*>
    ...
    include conf/common_rule.conf
</VirtualHost> 

<VirtualHost _default_:443>
    ... #SSL rules
    include conf/common_rule.conf
</VirtualHost>  

回答by Sampath Perera

You can use any # of hosts and ports in a single Virtualhost directive.

您可以在单个 Virtualhost 指令中使用任意数量的主机和端口。

<VirtualHost addr[:port] [addr[:port]] ...> ... </VirtualHost> 

In My case I used.

在我的情况下,我使用了。

<VirtualHost *:80 *:443>
  ServerName loop.lk

 ....
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/local.crt

</VirtualHost>

回答by RemyNL

Sorry to bump up an old post like this, but in order to help other Googlers out there I wanted to share how I dealt with it:

很抱歉碰到这样的旧帖子,但为了帮助其他谷歌员工,我想分享我是如何处理它的:

I have a couple of vhosts on my localhost, say: localhost, foo.com, bar.com

我有一对夫妇在我的本地虚拟主机的,说:localhostfoo.combar.com

This being a localhost site on my laptop (macosx) I could get away with self-signed certificates and thus the ssl-part is the same for all the vhosts...

这是我的笔记本电脑(macosx)上的本地主机站点,我可以使用自签名证书,因此所有虚拟主机的 ssl 部分都是相同的...

What I did is this:

我所做的是这样的:

I created the directory /etc/apache2/extra/vhosts/.

我创建了目录/etc/apache2/extra/vhosts/

I created a /etc/apache2/extra/vhosts/localhost.conf:

我创建了一个/etc/apache2/extra/vhosts/localhost.conf

ServerName localhost
DocumentRoot "/www/localhost"
<Directory /www/localhost>
  Require all granted
</Directory>
ErrorLog "/var/log/apache2/localhost.error_log"
CustomLog "/var/log/apache2/localhost.access_log" common

A /etc/apache2/extra/vhosts/foo.conf:

/etc/apache2/extra/vhosts/foo.conf

ServerName foo.com
DocumentRoot "/www/foo.com"
<Directory /www/foo.com>
  Require all granted
</Directory>
ErrorLog "/var/log/apache2/foo.com.error_log"
CustomLog "/var/log/apache2/foo.com.access_log" common

A /etc/apache2/extra/vhosts/bar.conf:

/etc/apache2/extra/vhosts/bar.conf

ServerName bar.com
DocumentRoot "/www/bar.com"
<Directory /www/bar.com>
  Require all granted
</Directory>
ErrorLog "/var/log/apache2/bar.com.error_log"
CustomLog "/var/log/apache2/bar.com.access_log" common

And finally a /etc/apache2/extra/vhosts/ssl.conf:

最后一个/etc/apache2/extra/vhosts/ssl.conf

SSLEngine on
SSLCertificateFile "/etc/apache2/ssl/server.crt"
SSLCertificateKeyFile "/etc/apache2/ssl/server.key"

And in my /etc/apache2/extra/httpd-vhosts.conf:

而在我的/etc/apache2/extra/httpd-vhosts.conf

<VirtualHost *:80>
  Include /etc/apache2/extra/vhosts/localhost.conf
</VirtualHost>
<VirtualHost *:443>
  Include /etc/apache2/extra/vhosts/localhost.conf
  Include /etc/apache2/extra/vhosts/ssl.conf
</VirtualHost>

<VirtualHost *:80>
  Include /etc/apache2/extra/vhosts/foo.conf
</VirtualHost>
<VirtualHost *:443>
  Include /etc/apache2/extra/vhosts/foo.conf
  Include /etc/apache2/extra/vhosts/ssl.conf
</VirtualHost>

<VirtualHost *:80>
  Include /etc/apache2/extra/vhosts/bar.conf
</VirtualHost>
<VirtualHost *:443>
  Include /etc/apache2/extra/vhosts/bar.conf
  Include /etc/apache2/extra/vhosts/ssl.conf
</VirtualHost>

回答by Seb

Another option instead of using Includeis using Macro(so you can keep it all in one file).

另一种不使用的选项Include是使用Macro(因此您可以将其全部保存在一个文件中)。

First enable the macro module:

首先启用宏模块:

a2enmod macro

Then put your shared stuff in a macro and useit from your virtualhosts:

然后将您共享的内容放在一个宏中,并将use其从您的虚拟主机中:

<Macro SharedStuff>
   ServerName example.com
   ServerAdmin [email protected]
   <DocumentRoot /var/www/example>
      ...
   </DocumentRoot>
</Macro>

<VirtualHost *:80>
  Use SharedStuff
</VirtualHost>

<VirtualHost *:443>
  Use SharedStuff

  SSLEngine On
  SSLProtocol All -SSLv2 -SSLv3
  ...
</VirtualHost>

Macros can also take parameters, and be defined in other files that are included; so you can use them a bit like Functions, and save a lot of duplication across your Apache config files.

宏也可以带参数,并在包含的其他文件中定义;所以你可以像使用函数一样使用它们,并在你的 Apache 配置文件中节省大量重复。

See here for more details:

请参阅此处了解更多详情:

https://httpd.apache.org/docs/2.4/mod/mod_macro.html

https://httpd.apache.org/docs/2.4/mod/mod_macro.html

回答by sme

You could put the common configuration into a separate file and include it in both VirtualHost segments. For example:

您可以将通用配置放入一个单独的文件中,并将其包含在两个 VirtualHost 段中。例如:

<VirtualHost 192.168.1.2:80>
  Include conf/common.conf
</VirtualHost>

<VirtualHost 192.168.1.2:443>
  Include conf/common.conf
  (put your ssl specific cofiguration stuff here ...)
</VirtualHost>

回答by Matt Jacob

You could also specify the common directives within a container instead of within the itself. That's what I do, mostly because I prefer mod_rewrite rules at the directory level instead of at the server level, but it should work equally well for you too.

您还可以在容器内而不是在容器本身内指定通用指令。这就是我所做的,主要是因为我更喜欢目录级别而不是服务器级别的 mod_rewrite 规则,但它也应该对您同样有效。