我正在研究图片托管 imgur clone。我为我的图片创建了一个子域 i.localhost 添加到 hosts 文件 -> 127.0.0.1 i.localhost 添加到 httpd-vhosts.conf ->
<VirtualHost *:80>
DocumentRoot "/Users/BillGates/Sites/images/"
ServerName i.localhost
</VirtualHost>
我的网站在 localhost 的本地机器上运行。访问者可以在 localhost/NE2bd 之类的 URL 上看到图片,我使用 Mod_Rewrite 处理了此 URL,因此它就像 imgur.com 一样
因为我将所有图像存储在 /Users/BillGates/Sites/images/ 中,所以我无法在 i.localhost/NE2bd.jpeg 中看到图像 !!!! 但是该图像位于 Images 文件夹中!!
我找不到
请求的 URL /NE2bd.jpeg 未在此服务器上找到。我的 httpd-vhosts.conf
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/BillGates/Sites/images/"
ServerName i.localhost
</VirtualHost>
当然,httpd.conf 中也有一些重要的行
DocumentRoot“/用户/比尔盖茨/网站”
答案1
看起来您正在 OSX 上使用 Apache 2,并且您正在编辑的文件是/etc/apache2/extra/httpd-vhosts.conf
。您复制和粘贴的某些代码似乎缺失了<VirtualHost *:80>
也许您添加的配置没有被读入 apache 配置。默认情况下不会读入此文件。您可以尝试编辑主文件/etc/apache2/httpd.conf
以包含此文件。
尝试取消注释该行:
Include /private/etc/apache2/extra/httpd-vhosts.conf.
当然,记得启动和停止 apache 以使其生效。
答案2
您的配置看起来不对。我习惯像这样声明一个 vhost
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /www/docs/host.foo.com
ServerName host.foo.com
</VirtualHost>
在包含在您的 httpd.conf 中的文件中。
我不确定您的配置是否被 Apache 使用,因为您的配置中不存在“localhost”-vhost。
尝试在 /etc/hosts 中添加域“foo.bar”和“i.foo.bar”,并将它们指向您的 IP。然后为每个域编写一个 VirtualHost 指令,并指定不同的 DocumentRoot。
之后,尝试访问位于您的(子)域的目录中的文件并发布错误(和配置)。