如何在 Ubuntu 上使用 Apache Web 服务器设置基于文件夹的虚拟主机 (Vhosts),例如 /localhost/site1 localhost/site2

如何在 Ubuntu 上使用 Apache Web 服务器设置基于文件夹的虚拟主机 (Vhosts),例如 /localhost/site1 localhost/site2

我正在尝试使用 codeigniter 根据 Ubuntu 18 上的 Apache2 文件夹配置具有多个站点的虚拟主机。我的意思是,site1 可以通过 url http://localhost/site1 访问,site2 可以通过 url http://localhost/site2 访问,site3 可以通过 url http://localhost/site3 访问,等等。要提供的内容位于 /var/www/site1 和 /var/www/site2 等文件夹中。我不能使用基于名称的虚拟主机,因为用户将通过网络而不是在本地计算机上使用它。

所以我更新了 ubuntu 上的默认配置文件,如下所示

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

    Alias /site1 /var/www/site1/html
    <Directory /var/www/site1/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
  </Directory>

    
           Alias /site2 /var/www/site2/html
    <Directory /var/www/site2/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

  Alias /site3 /var/www/site3/html
    <Directory /var/www/site3/html>
            Options +MultiViews -Indexes +Includes +FollowSymLinks
            AllowOverride All
            Require all granted
  </Directory>

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

文件夹结构如下 /var/www/site1/html /var/www/site2/html /var/www/site3/html 等。

我可以正常访问 site1,没有任何问题。但对于 site2 和 site3,我收到 500 内部服务器错误,日志中显示消息“由于可能的配置错误,请求超出了 10 次内部重定向的限制。如有必要,请使用“LimitInternalRecursion”增加限制。”。启用调试模式后,日志中显示以下消息。

: r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895466 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895478 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895490 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895501 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895512 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895523 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895534 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895562 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895573 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/html/index.php/master/get_announcements, referer: http://192.186.1.4/site2/
[Mon Feb 01 12:07:57.895583 2021] [core:debug] [pid 8335] core.c(3840): [client 192.186.1.3:57079] AH00122: redirected from r->uri = /site2/master/get_announcements, referer: http://192.186.1.4/site2/

如果有帮助的话,以下是 /var/www/site2 文件夹中的 .htaccess

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On


#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file

RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

这三个目录中都存在相同的 .htaccess。但它只对第一个有效,而对其他两个目录则会出现上述错误。

请帮我修复这个问题。

提前致谢

答案1

简短回答:尝试添加

RewriteBase /site2

在 site2 的 .htaccess 中。对于 /site1 和 /site3 也同样如此。

从错误日志来看,问题显然出在RewriteRule上,它创建了一个添加/html/index.php到URL前面的无限循环。

问题出在这种情况下:

RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond 尝试排除已经以 、 等开头的 URL index.phppublic在 .htaccess 文件中,除非您告诉 Apache 删除 URL 的前导部分(如上所示),否则此方法不起作用RewriteBase。请参阅文档重写库,以及文档中的“匹配的内容是什么?”和“按目录重写”部分重写规则,虽然警告,但这些部分确实很难理解。

我猜测该规则对 /site1 有效但对其他规则无效的原因是 /site1 包含一个 index.php 文件,因此条件RewriteCond %{REQUEST_FILENAME} !-f失败并且不会触发 RewriteRule。

顺便说一下,这里不需要<VirtualHost>,因为您从同一个主机名(http://localhost)为这 3 个目录提供服务。

相关内容