Apache 默认虚拟主机
如何在 Apache 中设置默认 VirtualHost?
最好,我希望默认主机不要与 IP 地址主机相同。现在我有这样的事情:
NameVirtualHost *
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
如果一个域被转发到我的服务器,但不在这个 vhost.conf
文件中,则加载 /someOtherDir/
中的文件,正如预期的那样。但我希望能够为 IP 地址本身和尚未添加到 vhost.conf
文件中的域使用不同的根。这可能吗?
How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost *
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf
file, the files from /someOtherDir/
are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf
file (yet). Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我找到了答案:我记得如果没有找到其他匹配的块,Apache 将使用第一个块,因此我在块的顶部添加了一个没有
serveralias
的块:I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a
serveralias
at the top of the blocks:如果您使用 Debian 风格的虚拟主机配置(sites-available/sites-enabled),设置默认 VirtualHost 的一种方法是首先在
httpd.conf
或apache 中包含特定的配置文件.conf
(或者您的主要配置文件)。If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in
httpd.conf
orapache.conf
(or what ever is your main configuration file).这里的其他答案对我不起作用,但我找到了一个非常简单的解决方案。
我将默认值设置为最后列出的一个,并为其指定了
ServerAlias *
。例如:
如果访问者没有明确选择访问以
secondwebsite.example
结尾的网站,他们将获得默认网站。The other answers here didn't work for me, but I found a pretty simple solution that did work.
I made the default one the last one listed, and I gave it
ServerAlias *
.For example:
If the visitor didn't explicitly choose to go to something ending in
secondwebsite.example
, they get the default website.实际上,我在 EC2 Linux AMI 上使用 Apache/2.4.39 (Amazon) 的虚拟主机配置(站点可用/站点启用)。因此,我有 1 个 EC2 实例来为许多站点(域)提供服务。
考虑到您已经安装并运行了虚拟主机。在我的文件夹
/etc/httpd/sites-available
中,我有一些带有域名的文件(后缀.conf
),例如:example.com.conf
。像这样创建一个新文件。sudo nano /etc/httpd/sites-available/example.com.conf
对于sites-available 中的每个
file.conf
,我创建一个符号链接:sudo ln -s /etc/httpd/sites-available/example.com.conf
/etc/httpd/sites-enabled/example.com.conf
这是默认配置,因此,如果直接通过服务器的IP访问,您将被重定向到sites-available文件夹中的第一个文件(
.conf
)的DocumentRoot,按文件名排序。要在通过 IP 访问时拥有默认 DocumentRoot 文件夹,您必须创建一个名为
0a.conf
的文件,然后 Apache 将为该站点提供服务,因为这个新文件将是第一个在站点可用文件夹中。您必须创建一个符号链接:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
要检查服务顺序,请使用它:
sudo apachectl -S
现在,重新启动 Apache,并检查它。
Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 EC2 instance to serve many sites (domains).
Considering that you already have Virtual Host installed and working. In my folder
/etc/httpd/sites-available
, I have some files with domain names (suffix.conf
), for example:example.com.conf
. Create a new file like that.sudo nano /etc/httpd/sites-available/example.com.conf
For each
file.conf
in sites-available, I create a symbolic link:sudo ln -s /etc/httpd/sites-available/example.com.conf
/etc/httpd/sites-enabled/example.com.conf
This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (
.conf
) in sites-available folder, sorted by filename.To have a default DocumentRoot folder when access by IP, you have to create a file named
0a.conf
, then Apache will serve this site because this new file will be the first in sites-available folder.You must create a symbolic link:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
To check serving order, use it:
sudo apachectl -S
Now, restart Apache, and check out it.
强制性的 - 以前的答案都不适合我。我继承了基于 IP 地址的虚拟主机和基于虚拟主机(未分配/捕获所有 IP 地址)的虚拟主机的奇怪组合,在此 Apache 配置中,ISPConfig。
我希望 Apache 能够为未配置的主机提供相同的页面。
我有:未配置的主机转到
000-default.conf
之后的第一个虚拟主机。不管我将 *:80 catch all 定义为第一个虚拟主机,Apache 都会加载第一个定义的站点,而不是默认的:虽然这不是完全有效的配置,但最终起作用的是添加一个基于 IP 地址的虚拟主机,而没有定义 ServerName/ServerAlias:
< code>$ apachectl -S 首先输出基于 IP 地址的虚拟主机,然后输出基于 * 的虚拟主机,最后我的默认站点在真实站点之前加载:
注意事项 - 在这样的配置中,* 虚拟主机不会工作,因此您需要将 IP 地址应用于所有虚拟主机。
Obligatory - none of the previous answers worked for me. I inherited a strange combination of IP address-based virtual hosts and * vhosts (not assigned/catch all IP addresses) based virtual hosts in this Apache configuration messed up by ISPConfig.
I wanted Apache to serve not configured hosts with the same page.
I had: not configured hosts went to the first vhost after
000-default.conf
. No matter I had *:80 catch all defined as the first vhost, instead of default Apache would load first defined site:Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:
$ apachectl -S
outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.
另一种设置是将默认虚拟主机放在配置文件的末尾而不是开头。这样,所有替代虚拟主机在与默认虚拟主机匹配之前都会被检查。
例子:
An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.
Example:
我有同样的问题。我可以通过在虚拟主机的 IncludeOptional 指令之前在 httpd.conf 本身中添加以下内容来修复它。现在localhost和IP 192.168.xx都指向Apache的默认测试页面。所有其他虚拟主机均按预期工作。
参考: https://httpd.apache.org/docs/ 2.4/vhosts/name-based.html#defaultvhost
I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.
Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost
NameVirtualHost 选项将是一个不错的选择。
The NameVirtualHost option would be a good option.
解决方案是:
就我而言,为了工作,我创建了一个名为
aaaa.example< 的
VirtualHost
(每个CNAME
没有VirtualHost
) /code> 因为我对不同的 VirtualHost 有不同的文件,并且知道 Apache 按字母顺序读取它们。The solution is:
In my case, to work, I created a
VirtualHost
(n.e.VirtualHost
perCNAME
) calledaaaa.example
since I have different files for differentVirtualHost
s and knowing that Apache reads them in alphabetical order.只有支持且正确的答案是:
或我自己的返回
403
的版本:Only supported and correct answer is:
or my own version to return
403
: