【问题标题】:Apache 2.4 multiple sites with different alias and pathApache 2.4 具有不同别名和路径的多个站点
【发布时间】:2018-12-26 20:55:54
【问题描述】:

我在本地网络中安装了 Ubuntu 16.04 和 apache 2.4.18。

我想拥有两个不同的网站,由/blah 标识,因为我想使用别名,例如:

192.168.2.134/wiki => /var/www/dokuwiki

192.168.2.134/sip => /var/www/rsip/public (Laravel)

为此,我已将 /etc/apache2/sites-available/dokuwiki.conf 配置为:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName 192.168.2.134:80/wiki
    Alias /wiki /var/www/dokuwiki
    <Directory /var/www/dokuwiki>
        Order deny,allow
        Allow from all
        Options FollowSymLinks
    </Directory>
</VirtualHost>

还有/etc/apache2/sites-available/rsip.conf 和:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName 192.168.2.134:80/sip
    Alias /sip /var/www/rsip/public
    <Directory /var/www/rsip/public/>
        AllowOverride All
        Require all granted
        Options FollowSymLinks
    </Directory>
</VirtualHost>

/etc/apache2/apache2.conf 中有:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options -Indexes
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

我已经启用了这两个站点并重新加载了 apache2。

apachectl -S 的结果是:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server 192.168.2.134 (/etc/apache2/sites-enabled/dokuwiki.conf:1)
         port 80 namevhost 192.168.2.134 (/etc/apache2/sites-enabled/dokuwiki.conf:1)
         port 80 namevhost 192.168.2.134 (/etc/apache2/sites-enabled/rsip.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: 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

apachectl -M 的结果是:

Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php7_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)

问题是,192.168.2.134/wiki 的链接一直都可以正常工作,但192.168.2.134/sip 的链接会显示404 File not found 并且映射到错误的路径,正如我在/var/log/apache2/error.log 中看到的那样:

[Wed Jul 18 22:00:16.601623 2018] [core:info] [pid 4686] [client 192.168.2.156:25896] AH00128: File does not exist: /var/www/sip/

如果我尝试192.168.2.134/rsip/public,它会起作用。

我认为这与虚拟主机的顺序有关,就像在执行apachectl -S 时看到的那样,如上所示..

然后,如果我执行a2dissite dokuwiki.conf 并重新加载 apache2,则指向 192.168.2.134/sip 的链接也可以正常工作。当我再次启用192.168.2.134/wiki 时,192.168.2.134/sip 再次不起作用。

它 - 一般 - 可以做,我正在尝试做什么?!

顺便说一句:/var/www/rsip/public/.htaccess 是:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        #Options -MultiViews -Indexes
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteBase /sip

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_URI} (.+)/$
#    RewriteRule ^ %1 [L,R=301]

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ dashboard/$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

【问题讨论】:

    标签: laravel apache ubuntu alias virtualhost


    【解决方案1】:

    ServerName 指令不应该有 /blah,而应该只有 [scheme://]domain-name|ip-address[:port]。你应该有这样的东西:

    <VirtualHost *:80>
        DocumentRoot /var/www/
        ServerName 192.168.2.134:80
        Alias /wiki /var/www/dokuwiki
        Alias /sip /var/www/rsip/public
        <Directory /var/www/dokuwiki>
            Order deny,allow
            Allow from all
            Options FollowSymLinks
        </Directory>
        <Directory /var/www/rsip/public/>
            AllowOverride All
            Require all granted
            Options FollowSymLinks
        </Directory>
    </VirtualHost>
    

    【讨论】:

    • 好吧,我把它缩减为 ip:port,重新加载,但仍然是 404 并且文件不存在:error.log 中的 /var/www/sip ... 有两个不同的 conf 文件是否重要(rsip.conf 和 dokuwiki.conf)作为站点可用?
    • 是的。我提供的配置是放在一个文件中,并且是唯一包含的文件。它配置了两个别名。
    • 非常感谢...它只适用于一个配置文件。您能否将此添加到您的答案中,只有一个配置文件很重要?
    • 嗨@DanielNe 如果用户只是在地址栏中输入 192.168.2.134 而后面没有任何路径,您的配置会起作用吗?
    • N@KelvinLowEeHahn 不,这行不通。我更改了它,因此在端口 80 上,您将被定向到一个应用程序(192.168.2.134),以及另一个具有别名的应用程序(192.168.2.134/wiki)
    【解决方案2】:

    不可能在同一个域(在本例中为 IP)上使用具有不同 DocumentRoot 的 2 个 VirtualHost。 Apache 将始终只使用多个已定义的 VirtualHost 之一。

    ServerName 只能是名称或 IP。端口已在 VirtauHost *:80 中定义。

    您可以使用一个 VirtualHost 作为您的 IP,并将 var/www 作为 DocumentRoot。 当两个站点都放在 /var/www 下时,不需要别名。 也许文件系统上的软链接比 Apache 中的别名更好。

    文件系统上的软链接可以使用:

    ln -s /var/www/rsip/public /var/www/sip
    

    【讨论】:

    • 可能安德烈·格利纳斯回答了它。还是谢谢!
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2019-05-05
    相关资源
    最近更新 更多