Apache从服务器主机名提供基于名称的虚拟主机(但不应该)

时间:2018-03-29 06:20:06

标签: apache virtualhost

我在Centos上运行了一个Apache httpd服务器(2.4.6),为几个基于名称的虚拟主机提供服务。服务器的fqdn / hostname应该不提供任何内容,但它会重定向到其中一个基于名称的虚拟主机,我不明白为什么,或者如何阻止它。

在下面的配置中,对http://host.mydomain.org/的请求被重定向到http://www.customer.co.uk/

任何有关原因的想法,以及阻止它适当的正确方法。

我的httpd.conf是:

ServerRoot "/etc/httpd"
Listen 80

Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@xxx.org
ServerName host.mydomain.org:80

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    Require all denied
</Directory>

<Directory "/var/www/html">
        Options None
    AllowOverride None
    Require all denied
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all denied
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddHandler cgi-script .cgi
    AddHandler cgi-script .pl
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset ISO-8859-1

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on
TraceEnable Off
ServerTokens Prod

IncludeOptional conf.d/*.conf

示例虚拟主机是:

<VirtualHost *:80>
    DocumentRoot /home/xxx/customer/docs
    ServerName www.customer.co.uk
    ErrorLog /home/xxx/customer/logs/error_log
    CustomLog /home/xxx/customer/logs/access_log combined
        <Directory /home/xxx/customer/docs>
                AllowOverride None
                Options None
                Require all granted
        </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName customer.co.uk
    Redirect permanent / http://www.customer.co.uk/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.customer.mydomain.org
    Redirect permanent / http://www.customer.co.uk/
</VirtualHost>

非常感谢 凯文

1 个答案:

答案 0 :(得分:0)

配置文件中的第一个VirtualHost具有最高优先级,可以视为默认服务器或主服务器。这意味着,如果收到的请求与指定的ServerNameServerAlias指令之一不匹配,则会由第一个<VirtualHost>提供。

您可以通过将此(例如)添加为第一个VirtualHost来阻止此操作:

<VirtualHost *:80>
    ServerName default
    RewriteEngine On
    RewriteRule ^ - [F]
</VirtualHost>