我感兴趣的是,如果我可以在 apache 上使用以下域名的虚拟主机: http://something.com/something 或 http://{server-ip-address-here}/something ?
我在 Ubuntu 服务器上使用 Apache 2.2.20,那是我的家庭服务器,我正在这里测试一些东西,我这里没有任何 DNS 服务器,我只有公共(public) IP 地址和附加的域名开启DNS服务。
那么,我做了什么:
- 我在 /etc/apache2/sites-available 中创建了新文件“demo”
我把它放在那里(实际上它是从默认文件修改复制过来的):
<pre><code><VirtualHost *:80> ServerAdmin webmaster@localhost ServerName {mydomain-here}/demo/ DocumentRoot /vhosts/demo <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /vhosts/demo/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost> </code></pre>
在/etc/apache2/sites-enabled/中创建指向/etc/apache2/sites-available/demo 的符号链接(symbolic link)
已创建 /vhosts/demo/index.html 文件。
现在我得到的是,当我转到 {my-domain} 时,我转到了我创建的虚拟主机,但问题是服务器在任何情况下都会将我指向那里,而不仅仅是 {my-domain}/demo what我要。
总而言之,我希望我可以创建不同的虚拟主机并将它们附加到具有相同基本 URL 的不同 URL,例如 www.mydomain.com/vhost1、www.mydomain.com/vhost2 等。
这可能吗? 谢谢:)
最佳答案
首先,出现任何情况的原因是您的虚拟主机有 *:80 设置,因此如果没有匹配请求,它会使用第一个虚拟主机条目
如果我理解你正在尝试做什么,那么看起来你可能只想为每个“虚拟主机”起个别名
您尝试做的并不完全是虚拟主机(至少虚拟主机应该做的),但您可以通过使用 apache 的别名功能来完成它
Alias /vhost1 /whatever/folder/your/vhost1/site/is/at
Alias /vhost2 /whatever/folder/your/vhost2/site/is/at
现在无论您使用什么域,例如http://whatever.com/vhost1或 http://whatever.com/vhost2 这两个 em 将显示为单独的站点
关于apache - apache 上具有 URL 之类子文件夹的虚拟主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505689/