13

我感兴趣的是,如果我可以在 apache 上使用域名如下的虚拟主机: http://something.com/somethinghttp://{server-ip-address-here}/something

我在 Ubuntu Server 上使用 Apache 2.2.20,那是我的家庭服务器,我在这里测试一些东西,我这里没有任何 DNS 服务器,我只有公共 IP 地址和一个从开放 dns 服务附加到它的域名.

那么,我做了什么:

  1. 我在/etc/apache2/sites-available中创建了新文件“demo”
  2. 我把它放在那里(实际上它是通过默认文件的修改复制的):

    <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>
    

  3. 在 /etc/apache2/sites-enabled/ 中创建了指向 /etc/apache2/sites-available/demo 的符号链接

  4. 创建/vhosts/demo/index.html文件。

现在我得到的是,当我转到 {my-domain} 时,我转到了我创建的虚拟主机,但问题是服务器在任何情况下都将我指向那里,而不仅仅是 {my-domain}/demo 我想要的。

总之,我希望我可以创建不同的虚拟主机并将它们附加到具有相同基本 URL 的不同 URL,例如 www.mydomain.com/vhost1、www.mydomain.com/vhost2 等。

是否可以?谢谢 :)

4

1 回答 1

16

首先,它出现在任何情况下的原因是因为你有一个 *:80 为你的虚拟主机设置,所以如果没有匹配的请求它使用第一个虚拟主机条目

如果我了解您要执行的操作,您可能只想为每个“虚拟主机”起别名

您尝试做的不是虚拟主机(至少虚拟主机应该做的),但您可以通过使用 apache 的别名功能来完成它

Alias /vhost1 /whatever/folder/your/vhost1/site/is/at
Alias /vhost2 /whatever/folder/your/vhost2/site/is/at

所以现在无论您使用什么域,例如http://whatever.com/vhost1http://whatever.com/vhost2 这两个都将显示为单独的站点

于 2012-02-29T20:21:49.740 回答