Apache 默认虚拟主机

发布于 2024-10-26 20:33:38 字数 819 浏览 7 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

浅暮の光 2024-11-02 20:33:38

我找到了答案:我记得如果没有找到其他匹配的块,Apache 将使用第一个块,因此我在块的顶部添加了一个没有 serveralias 的块:

NameVirtualHost *

<VirtualHost *>
    DocumentRoot /defaultdir/
</VirtualHost>

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot /someOtherDir/
    ServerAlias ip.of.the.server
</VirtualHost>

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot /someroot/
    ServerAlias example.com *.example.com
</VirtualHost>

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:

NameVirtualHost *

<VirtualHost *>
    DocumentRoot /defaultdir/
</VirtualHost>

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot /someOtherDir/
    ServerAlias ip.of.the.server
</VirtualHost>

<VirtualHost *>
    ServerAdmin [email protected]
    DocumentRoot /someroot/
    ServerAlias example.com *.example.com
</VirtualHost>
战皆罪 2024-11-02 20:33:38

如果您使用 Debian 风格的虚拟主机配置(sites-available/sites-enabled),设置默认 VirtualHost 的一种方法是首先在 httpd.confapache 中包含特定的配置文件.conf(或者您的主要配置文件)。

# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.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 or apache.conf (or what ever is your main configuration file).

# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf

# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf
梦里人 2024-11-02 20:33:38

这里的其他答案对我不起作用,但我找到了一个非常简单的解决方案。

我将默认值设置为最后列出的一个,并为其指定了 ServerAlias *

例如:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.secondwebsite.example
    ServerAlias secondwebsite.example *.secondwebsite.example
    DocumentRoot /home/secondwebsite/web
</VirtualHost>

<VirtualHost *:80>
    ServerName www.defaultwebsite.example
    ServerAlias *
    DocumentRoot /home/defaultwebsite/web
</VirtualHost>

如果访问者没有明确选择访问以 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:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.secondwebsite.example
    ServerAlias secondwebsite.example *.secondwebsite.example
    DocumentRoot /home/secondwebsite/web
</VirtualHost>

<VirtualHost *:80>
    ServerName www.defaultwebsite.example
    ServerAlias *
    DocumentRoot /home/defaultwebsite/web
</VirtualHost>

If the visitor didn't explicitly choose to go to something ending in secondwebsite.example, they get the default website.

傲世九天 2024-11-02 20:33:38

实际上,我在 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

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/html/domain
</VirtualHost>

对于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

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/html/domain
</VirtualHost>

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.

土豪我们做朋友吧 2024-11-02 20:33:38

强制性的 - 以前的答案都不适合我。我继承了基于 IP 地址的虚拟主机和基于虚拟主机(未分配/捕获所有 IP 地址)的虚拟主机的奇怪组合,在此 Apache 配置中,ISPConfig

我希望 Apache 能够为未配置的主机提供相同的页面。

我有:未配置的主机转到 000-default.conf 之后的第一个虚拟主机。不管我将 *:80 catch all 定义为第一个虚拟主机,Apache 都会加载第一个定义的站点,而不是默认的:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
</VirtualHost>

虽然这不是完全有效的配置,但最终起作用的是添加一个基于 IP 地址的虚拟主机,而没有定义 ServerName/ServerAlias:

<VirtualHost 192.168.10.10:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost 192.168.10.10:443>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  SSLEngine On
  ...
</VirtualHost>

< code>$ apachectl -S 首先输出基于 IP 地址的虚拟主机,然后输出基于 * 的虚拟主机,最后我的默认站点在真实站点之前加载:

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80        is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)

...

46.23.86.103:443       is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)

...

*:80                   is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)

注意事项 - 在这样的配置中,* 虚拟主机不会工作,因此您需要将 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:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
</VirtualHost>

Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:

<VirtualHost 192.168.10.10:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost 192.168.10.10:443>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html
  SSLEngine On
  ...
</VirtualHost>

$ apachectl -S outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:

AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80        is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
         port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)

...

46.23.86.103:443       is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
         port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)

...

*:80                   is a NameVirtualHost
         default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)

Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.

青春如此纠结 2024-11-02 20:33:38

另一种设置是将默认虚拟主机放在配置文件的末尾而不是开头。这样,所有替代虚拟主机在与默认虚拟主机匹配之前都会被检查。

例子:

NameVirtualHost *:80
Listen 80

...

<VirtualHost *:80>
        ServerName host1
        DocumentRoot /someDir
</VirtualHost>

<VirtualHost *:80>
        ServerName host2
        DocumentRoot /someOtherDir
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot /defaultDir
</VirtualHost>

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:

NameVirtualHost *:80
Listen 80

...

<VirtualHost *:80>
        ServerName host1
        DocumentRoot /someDir
</VirtualHost>

<VirtualHost *:80>
        ServerName host2
        DocumentRoot /someOtherDir
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot /defaultDir
</VirtualHost>
潜移默化 2024-11-02 20:33:38

我有同样的问题。我可以通过在虚拟主机的 IncludeOptional 指令之前在 httpd.conf 本身中添加以下内容来修复它。现在localhost和IP 192.168.xx都指向Apache的默认测试页面。所有其他虚拟主机均按预期工作。

<VirtualHost *:80>
    DocumentRoot /var/www/html
</VirtualHost>

参考: 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.

<VirtualHost *:80>
    DocumentRoot /var/www/html
</VirtualHost>

Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost

木緿 2024-11-02 20:33:38

NameVirtualHost 选项将是一个不错的选择。

The NameVirtualHost option would be a good option.

桃酥萝莉 2024-11-02 20:33:38

解决方案是:

# apache2.conf

# @warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80

# ...
# aaaa.example.conf

<VirtualHost *:80>
    ServerName aaaa.example
    DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf

<VirtualHost *:80>
    ServerName host1.example
    DocumentRoot /someDir
</VirtualHost>
# host2.example.conf

<VirtualHost *:80>
    ServerName host2.example
    DocumentRoot /someOtherDir
</VirtualHost>

就我而言,为了工作,我创建了一个名为 aaaa.example< 的 VirtualHost(每个 CNAME 没有 VirtualHost) /code> 因为我对不同的 VirtualHost 有不同的文件,并且知道 Apache 按字母顺序读取它们。

The solution is:

# apache2.conf

# @warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80

# ...
# aaaa.example.conf

<VirtualHost *:80>
    ServerName aaaa.example
    DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf

<VirtualHost *:80>
    ServerName host1.example
    DocumentRoot /someDir
</VirtualHost>
# host2.example.conf

<VirtualHost *:80>
    ServerName host2.example
    DocumentRoot /someOtherDir
</VirtualHost>

In my case, to work, I created a VirtualHost (n.e. VirtualHost per CNAME) called aaaa.example since I have different files for different VirtualHosts and knowing that Apache reads them in alphabetical order.

君勿笑 2024-11-02 20:33:38

只有支持且正确的答案是:

<VirtualHost _default_:*>
    DocumentRoot "/www/default"
</VirtualHost>

或我自己的返回 403 的版本:

<VirtualHost _default_:*>
    <Location />
        Require all denied
    </Location>
</VirtualHost>

Only supported and correct answer is:

<VirtualHost _default_:*>
    DocumentRoot "/www/default"
</VirtualHost>

or my own version to return 403:

<VirtualHost _default_:*>
    <Location />
        Require all denied
    </Location>
</VirtualHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文