Apache2 子域名无法加载内容(镜像主域名)

Apache2 子域名无法加载内容(镜像主域名)

我现在在 Ubuntu 上运行着一个 Apache2 网络服务器。主域名运行正常(示例.com)。我正在尝试添加子域名(子域名.example.com),但我无法让它工作。

DNS一切设置正确。我可以 ping子域名.example.com。但当我导航到 URL 时,它只是镜像主域示例.com

在 中/etc/apache2/sites-available,我有两个文件:001-subdomain.example.com.conf999-example.com.conf

001-subdomain.example.com.conf看起来像这样:

<VirtualHost *:80>
    ServerAdmin [email protected]

    DocumentRoot /var/www/subdomain.example.com/html
    DirectoryIndex index.php index.html

    ServerName subdomain.example.com
    ServerAlias subdomain.example.com *.subdomain.example.com

    ErrorLog ${APACHE_LOG_DIR}/subdomain-error.log
    CustomLog ${APACHE_LOG_DIR}/subdomain-access.log combined
    
</VirtualHost>

<VirtualHost *:443>

    ServerAdmin [email protected]

    DocumentRoot /var/www/subdomain.example.com/html
    DirectoryIndex index.php index.html

    ServerName subdomain.example.com
    ServerAlias subdomain.example.com *.subdomain.example.com
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on

    SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
                   nokeepalive ssl-unclean-shutdown \
                   downgrade-1.0 force-response-1.0
</VirtualHost>

<Directory /var/www/subdomain.example.com/html>
    Options FollowSymLinks
    AllowOverride All

    Order Allow,Deny
    Allow From All
</Directory>

999-example.com.conf看起来像这样:

<VirtualHost *:80>
    ServerAdmin [email protected]
    
    DocumentRoot /var/www/example.com/html
    DirectoryIndex index.php index.html

    ServerName example.com
    ServerAlias example.com *.example.com *.*
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
</VirtualHost>

<VirtualHost *:443>

    ServerAdmin [email protected]
    
    DocumentRoot /var/www/example.com/html
    DirectoryIndex index.php index.html

    ServerName example.com
    ServerAlias example.com *.example.com *.*
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on

    SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
                    SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
                    SSLOptions +StdEnvVars
    </Directory>

    BrowserMatch "MSIE [2-6]" \
                   nokeepalive ssl-unclean-shutdown \
                   downgrade-1.0 force-response-1.0
</VirtualHost>

<Directory /var/www/example.com/html>
    Options FollowSymLinks
    AllowOverride All

    Order Allow,Deny
    Allow From All
</Directory>

但是,子域名仍然没有任何内容。它只是主站点的镜像。正确的文件/var/www/subdomain.example.com/html

如何设置子域名以加载正确的内容?

答案1

Apache 按字母顺序加载内容,因此,如果子域名配置为主域,对子域的请求很有可能被错误地路由到主域。通常,我对 Apache 所做的是将主站点放在配置文件列表的最后。例如:

/etc/apache/sites-enabled$ ll

drwxr-xr-x 2 root root 4096  2月  3 16:33 ./
drwxr-xr-x 8 root root 4096  1月 22 13:07 ../
lrwxrwxrwx 1 root root   34  1月 28 21:49 002-subdomain1.conf -> ../sites-available/002-subdomain1.conf
lrwxrwxrwx 1 root root   33  2月  3 16:33 003-subdomain2.conf -> ../sites-available/003-subdomain2.conf
lrwxrwxrwx 1 root root   32  1月 22 17:39 999-primary.conf -> ../sites-available/999-primary.conf

每个配置文件都包含一个带有通配符的ServerNameServerAlais(在适当的情况下)。这意味着子域.conf文件可能看起来像这样:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/subdomain1/html

        ServerName sub1.domain.com
        ServerAlias sub1.domain.com *.sub1.domain.com
        DirectoryIndex index.php index.html

        ErrorLog ${APACHE_LOG_DIR}/subdomain1-error.log
        CustomLog ${APACHE_LOG_DIR}/subdomain1-access.log combined
</VirtualHost>

<VirtualHost *:443>...</VirtualHost>

<Directory /var/www/subdomain1/html>
        Options FollowSymLinks
        AllowOverride All

        Order Allow,Deny
        Allow From All
</Directory>

主站点(也是默认站点)会有一个.conf类似的文件,但有一个更广泛的列表来ServerAlias捕获任何意外流量:

        ServerAlias domain.com *.domain.com *.*

多年来,这一做法一直很有效

相关内容