Apache选项-Indexes配置无效

10
我需要停止网站上图片目录的目录列表。我正在为网站上的图像和JavaScript配置无cookie域。我已经完成了CNAME配置,并在httpd.conf文件中添加了下面的虚拟主机配置。但是,如果我直接访问这个无cookie域,它会列出整个目录内容。如何解决这个问题?
``` ServerAdmin webmaster@site.com ServerName imgs.site.com ServerAlias www.imgs.site.com DocumentRoot /usr/tomcat/webapps/site/images
Options -Indexes FollowSymLinks AllowOverride none CustomLog logs/imgs.site.com_access_log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" ErrorLog logs/imgs.site.com_error_log ServerAdmin webmaster@site.com ServerName imgs.site.com ServerAlias www.imgs.site.com imgs.site.net DocumentRoot /usr/tomcat/webapps/site/images
Options -Indexes FollowSymLinks AllowOverride none CustomLog logs/imgs.site.com_access_log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" ErrorLog logs/imgs.site.com_error_log ```
3个回答

11
Options -Indexes FollowSymLinks

来自Apache 2.0和Apache 2.2文档

警告
将带有+或-的选项与没有带有这些选项的选项混合使用不是有效的语法,并且可能会导致意外结果。

Apache 2.4中,这将被...

...在语法检查期间由服务器启动拒绝并中止。

因此,您基本上需要在FollowSymLinks前面加上+(或者完全删除-Indexes参数,如果要覆盖所有先前定义的选项)。例如:

Options -Indexes +FollowSymLinks

我想知道如果我们不想进行目录索引,是否有必要使用-Indexes。我们不能只是不包括Indexes吗?并且像这样设置选项:Options FollowSymLinks - Ashish Ranjan
@AshishRanjan 是的,你可以这样做。但请注意,这也会覆盖之前在服务器配置中定义的任何其他选项(虽然,由于这是在VirtualHost上下文中,这可能是更好的选择)。 - MrWhite
好的,所以 + 表示添加而不是覆盖服务器配置中可能已定义的现有选项! - Ashish Ranjan
@AshishRanjan 是的,没错。因此,在这个问题的背景下,我们不能可靠地建议使用 Options FollowSymLinks,因为我们不知道服务器配置文件中之前已经定义了哪些其他选项(可能正在使用)。 - MrWhite

9

我认为在Directory指令中的路径是附加到DocumentRoot上的,所以你实际上是命令Apache不要索引/usr/tomcat/webapps/site/images/usr/tomcat/webapps/site/images。请尝试以下配置:

DocumentRoot /usr/tomcat/webapps/site

<Directory ~ "/.*/">
    Options -Indexes
</Directory>

这应该会禁用/usr/tomcat/webapps/site下所有文件夹的目录索引,例如/usr/tomcat/webapps/site/images//usr/tomcat/webapps/site/fubar/等等。

3
我认为在Directory指令中的路径被追加到DocumentRoot。但我错了: "Directory-path是一个目录的完整路径,或者使用类Unix shell匹配符号的通配符字符串" (http://httpd.apache.org/docs/2.2/mod/core.html#directory)。所以,我的示例之所以有效,仅仅是因为它匹配了主机中的*每个目录*。 - hudolejev

2
一个快速的解决方法是在目录中放置一个名为index.html的文件,并填入任意内容。这样就会显示该文件的内容,而不是目录列表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接