Apache2 - 始终提供默认 VHOST

Posted

技术标签:

【中文标题】Apache2 - 始终提供默认 VHOST【英文标题】:Apache2 - always serves default VHOST 【发布时间】:2017-10-17 01:18:27 【问题描述】:

我知道有很多问题/答案,但没有一个能解决我的问题。

我的设置: - 我有 2 个子域(sub1.test.de 和 sub2.test.de),它们被配置为将代理重定向到相同的 IP(我的虚拟机的 IP,在其他地方) - 我的虚拟主机有两个配置文件 - 虚拟机正在运行 Ubuntu 14.04

我尝试设置NameVirtualHost *:80,将所有虚拟主机放在一个配置文件中而不是单独的配置文件中,在ports.conf 中设置NameVirtualHost *:80 并指定IP 而不是通配符,但没有成功。打开sub2.test.de 仍然会将我重定向到sub1.test.de

这是我的sub1.test.de.conf:

<VirtualHost *:80>
    ServerName sub1.test.de

    ServerAdmin webmaster@test.de
    DocumentRoot /var/www/sub1.test.de/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    LogLevel info

    ErrorLog /var/www/sub1.test.de/log/error.log
    CustomLog /var/www/sub1.test.de/log/access.log combined

    # Wildfly api Prox Bypass
    ProxyPass /api http://someip:8080/api
    ProxyPassReverse /api http://someip:8080/api

    # Websocket Proxy Bypass
    RewriteEngine on
    RewriteCond %HTTP:UPGRADE ^WebSocket$ [NC]
    RewriteCond %HTTP:CONNECTION ^Upgrade$ [NC]
    RewriteRule .* ws://sub1.test.de:9000%REQUEST_URI [P]

    ProxyPass /socket http://someip:9000/socket
    ProxyPassReverse /socket http://someip:9000/socket

    <Directory /var/www/sub1.test.de/html>

        # Enable rewriting
        RewriteEngine On

        # If its not HTTPS
        # RewriteCond %HTTPS off

        # Comment out the RewriteCond above, and uncomment the RewriteCond below if you're using a load balancer (e.g. CloudFlare) for SSL
        # RewriteCond %HTTP:X-Forwarded-Proto !https

        # Redirect to the same URL with https://, ignoring all further rules if this one is in effect
        #RewriteRule ^(.*) https://%HTTP_HOST/$1 [R,L]

        # If we get to here, it means we are on https://

        # If the file with the specified name in the browser doesn't exist
        RewriteCond %REQUEST_FILENAME !-f

        # and the directory with the specified name in the browser doesn't exist
        RewriteCond %REQUEST_FILENAME !-d

        # and we are not opening the root already (otherwise we get a redirect loop)
        RewriteCond %REQUEST_FILENAME !\/$

        # Rewrite all requests to the root
        RewriteRule ^(.*) /

    </Directory>

这是我的sub1.test.de.conf

<VirtualHost *:80>
    ServerName sub2.test.de

    ServerAdmin webmaster@test.de
    DocumentRoot /var/www/sub2.test.de/html


    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    LogLevel info

    ErrorLog /var/www/sub2.test.de/log/error.log
    CustomLog /var/www/sub2.test.de/log/access.log combined

    # Wildfly api Prox Bypass
    ProxyPass /api http://someip:8080/api
    ProxyPassReverse /api http://someip:8080/api

    <Directory /var/www/sub2.test.de/html>

        # Enable rewriting
        RewriteEngine On

        # If its not HTTPS
        # RewriteCond %HTTPS off

        # Comment out the RewriteCond above, and uncomment the RewriteCond below if you're using a load balancer (e.g. CloudFlare) for SSL
        # RewriteCond %HTTP:X-Forwarded-Proto !https

        # Redirect to the same URL with https://, ignoring all further rules if this one is in effect
        #RewriteRule ^(.*) https://%HTTP_HOST/$1 [R,L]

        # If we get to here, it means we are on https://

        # If the file with the specified name in the browser doesn't exist
        RewriteCond %REQUEST_FILENAME !-f

        # and the directory with the specified name in the browser doesn't exist
        RewriteCond %REQUEST_FILENAME !-d

        # and we are not opening the root already (otherwise we get a redirect loop)
        RewriteCond %REQUEST_FILENAME !\/$

        # Rewrite all requests to the root
        RewriteRule ^(.*) /

    </Directory>

apachectl -t -D DUMP_VHOSTS 给我以下输出:

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 2a01:4f8:c0c:365::2. Set the 'ServerName' directive globally to suppress this message

VirtualHost configuration:

*:80                   is a NameVirtualHost
         default server sub1.test.de (/etc/apache2/sites-enabled/sub1.test.conf:3)
         port 80 namevhost sub1.test.de (/etc/apache2/sites-enabled/sub1.test.de.conf:3)
         port 80 namevhost sub2.test.de (/etc/apache2/sites-enabled/sub2.test.de.conf:1)

不过,sub2.test.de 始终对我打开 sub1.test.de。我做错了什么?

【问题讨论】:

【参考方案1】:

我解决了。问题是,我有一个常规的重定向条目,来自我的域列出的位置,而不是 A-NAME 记录。在我将重定向更改为 A-NAME 记录并等待几个小时以刷新 DNS 后,一切正常。

【讨论】:

以上是关于Apache2 - 始终提供默认 VHOST的主要内容,如果未能解决你的问题,请参考以下文章

未提供 Apache2 默认主机 - ispconfig

VirtualHost 始终在 Ubuntu 14.04 上使用 Apache 返回默认主机

Apache2 反向代理上的 Odoo 14 - 页面速度错误始终如一

强制访问者默认使用 SSL - Apache2

Apache2 vHost conf - 重定向到不同的服务器

Apache 2.4 虚拟主机被忽略