Apache Virtualhost 更喜欢新站点的“默认”服务器

Posted

技术标签:

【中文标题】Apache Virtualhost 更喜欢新站点的“默认”服务器【英文标题】:Apache Virtualhost preferring the "default" server for new site 【发布时间】:2021-03-17 18:37:04 【问题描述】:

我有一个运行 Ubuntu 的 DigitalOcean droplet。

在它上面,我使用 Apache 托管了三个站点。我正在尝试添加第四个“newsite.com”,从另一个主机交换。这第四个站点在另一个启用了 SSL 的环境中运行。

现有的三个站点运行良好。他们使用 LetsEncrypt 启用了 SSL,我使用虚拟主机设置具有相同 IP 的多个域。但是,对于第四个,新站点,如果我尝试查看该站点(通过编辑我的 Win10 主机文件以指向服务器 IP),我会看到其他三个站点之一,“oldsite1.com”,它是按字母顺序排列的列表中最高的。我了解 Apache 在启用的站点列表中向下工作以选择一个。新站点未启用 SSL,因为在更改 DNS 之前我无法运行 LetsEncrypt。

新站点的 .conf 文件如下所示:

<VirtualHost *:80>
        ServerAdmin webmaster@newsite.com
        ServerName newsite.com
        ServerAlias www.newsite.com
        DocumentRoot /var/www/newsite.com/public_html

        <Directory /var/www/newsite.com/public_html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        ErrorLog $APACHE_LOG_DIR/error.log
        CustomLog $APACHE_LOG_DIR/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

RewriteEngine on
RewriteCond %SERVER_NAME =newsite.com [OR]
RewriteCond %SERVER_NAME =www.newsite.com
RewriteRule ^ http://%SERVER_NAME%REQUEST_URI [END,NE,R=permanent]
</VirtualHost>

这与其他站点的 .conf 文件匹配。

我的新闻站点 .htaccess 文件:

#Force SSL
Options +FollowSymlinks
RewriteEngine On
RewriteCond %SERVER_PORT 80 
RewriteRule ^(.*)$ https://newsite.com/$1 [R=301,L]

# Prevent Directory listing
Options -Indexes

#Whitelist IP
#RewriteEngine On
#RewriteCond %REMOTE_ADDR [removed]
#RewriteCond %DOCUMENT_ROOT/error.html -f
#RewriteCond %DOCUMENT_ROOT/error.enable -f
#RewriteCond %SCRIPT_FILENAME !error.html
#RewriteRule ^.*$ /error.html [R=503,L]
#ErrorDocument 503 /error.html
#Header Set Cache-Control "max-age=0, no-store"

# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
 Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_URI !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

### Additional Settings that may need to be enabled for some servers
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

# 7. disable open_basedir limitations
# php_admin_value open_basedir none

###Compression stuff###
# Enable Gzip Compression
AddOutputFilterByType DEFLATE text/text
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Leverage Browser Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 1 month"
</IfModule>

# Remove Browser Bugs
#BrowserMatch ^Mozilla/4 gzip-only-text/html
#BrowserMatch ^Mozilla/4\.0[678] no-gzip
#BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
#Header append Vary User-Agent

运行sudo apache2ctl -S 返回:

VirtualHost configuration:

*:443                  is a NameVirtualHost
         default server oldsite1.com (/etc/apache2/sites-enabled/oldsite1.com-le-ssl.conf:2)
         port 443 namevhost oldsite1.com (/etc/apache2/sites-enabled/oldsite1.com-le-ssl.conf:2)
                 alias www.oldsite1.com
         port 443 namevhost oldsite2.com (/etc/apache2/sites-enabled/oldsite2.com-le-ssl.conf:2)
                 alias www.oldsite2.com
         port 443 namevhost oldsite3.com (/etc/apache2/sites-enabled/oldsite3.com-le-ssl.conf:2)
                 alias www.oldsite3.com
*:80                   is a NameVirtualHost
         default server 127.0.0.1 (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost 127.0.0.1 (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost oldsite1.com (/etc/apache2/sites-enabled/oldsite1.com.conf:1)
                 alias www.oldsite1.com
         port 80 namevhost oldsite2.com (/etc/apache2/sites-enabled/oldsite2.com.conf:1)
                 alias www.oldsite2.com
         port 80 namevhost oldsite3.com (/etc/apache2/sites-enabled/oldsite3.com.conf:1)
                 alias www.oldsite3.com
         port 80 namevhost newsite.com (/etc/apache2/sites-enabled/newsite.com.conf:1)
                 alias www.newsite.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

我已尝试清空 newsite .htaccess 并多次重新启动 apache,但当我尝试查看 newsite.com 时总是显示 oldsite1.com。

假设我的 .conf 或 .htaccess 没有问题,为什么 Apache 更喜欢列表中第一个站点的虚拟主机?

【问题讨论】:

【参考方案1】:

您正在强制使用 ssl。在您加载 http://newsite.com 的那一刻,您将被重定向到 https://newsite.com

您在端口 80 上只有一个 newsite.com 的虚拟主机,而不是在端口 443 上。

由于端口 443 上没有请求域的虚拟主机,因此您将获得默认站点。

如果您注释掉这部分,您将按预期获得您的网站:

#Force SSL
RewriteEngine On
#RewriteCond %SERVER_PORT 80 
#RewriteRule ^(.*)$ https://newsite.com/$1 [R=301,L]

【讨论】:

不错的答案@Thakkie!

以上是关于Apache Virtualhost 更喜欢新站点的“默认”服务器的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 Google 将地址从旧站点更改为新站点

添加Apache VirtualHost,localhost的DocumentRoot更改为VirtualHost的DocumentRoot

如何使用 Apache VirtualHost 获取 .htaccess 文件

如何使用 Apache VirtualHost 获取 .htaccess 文件

将 .asp 页面重定向到我的新站点的主页

当我在 VirtualHost 中更改 ServerName 时,Apache (xampp) 不起作用