我正在 VPS 上运行我的网站。我们将其命名为 example.com。我想添加一个子域名 sub.example.com。我添加了一条 A 记录,创建了文件夹 /var/www/sub 并添加了 VirtualHost 部分。/apache2/sites-available 中的配置文件现在如下所示:
<VirtualHost *:80>
ServerName example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/sub/
<Directory /var/www/sub>
Allow from all
Options +Indexes
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
不幸的是,这不起作用。每当我浏览此子域时,我都会被重定向到主站点。似乎 Apache 不会将我重定向到子文件夹及其内容。我该如何让它工作?
哦,这是输出apache2ctl -S
:
VirtualHost configuration:
*:443 example.com (/etc/apache2/sites-enabled/000-default-le-ssl.conf:2)
*:80 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost sub.example.com (/etc/apache2/sites-enabled/000-default.conf:36)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: 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
答案1
我通过将设置应用于端口 443 的配置文件来修复此问题。我完全忘记提到这一点(以及配置文件本身)。
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName sub.example.com
ServerAlias www.sub.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/app
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>