Apache 选项 - 索引配置不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1988267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:32:59  来源:igfitidea点击:

Apache Options -Indexes configuration not working

apachehttpd.conf

提问by techiepark

I need to stop directory listing of images directory on a website. I'm configuring cookieless domain for images and javascripts on a site. I have done the CNAME configuration and added below virtual hosts configuration in httpd.conf file. But, if i access this cookieless domain directly, its listing the whole directory content. how to solve this problem?

我需要停止网站上图像目录的目录列表。我正在为网站上的图像和 javascript 配置 cookieless 域。我已经完成了 CNAME 配置并在 httpd.conf 文件中添加了以下虚拟主机配置。但是,如果我直接访问这个 cookieless 域,它会列出整个目录内容。如何解决这个问题呢?

<VirtualHost ipaddr:80>
    ServerAdmin [email protected]
    ServerName imgs.site.com
    ServerAlias www.imgs.site.com
    DocumentRoot /usr/tomcat/webapps/site/images

    <Directory /usr/tomcat/webapps/site/images>
       Options -Indexes FollowSymLinks
       AllowOverride none
    </Directory>

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

<VirtualHost ipaddr:80>
    ServerAdmin [email protected]
    ServerName imgs.site.com
    ServerAlias www.imgs.site.com imgs.site.net
    DocumentRoot /usr/tomcat/webapps/site/images

    <Directory /usr/tomcat/webapps/site/images>
       Options -Indexes FollowSymLinks
       AllowOverride none
    </Directory>

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

采纳答案by Zed

A quick workaround is to put an index.htmlfile into the directory, with arbitrary content. Indexing will display the contents of this file instead of the directory listing.

一个快速的解决方法是将一个index.html文件放入目录中,其中包含任意内容。索引将显示此文件的内容而不是目录列表。

回答by hudolejev

I think that path in Directorydirective is appended to DocumentRoot, so you actually ordering Apache not to index /usr/tomcat/webapps/site/images/usr/tomcat/webapps/site/images. Try the following configuration instead:

我认为Directory指令中的路径已附加到DocumentRoot,因此您实际上命令 Apache 不索引/usr/tomcat/webapps/site/images/usr/tomcat/webapps/site/images. 请尝试以下配置:

DocumentRoot /usr/tomcat/webapps/site

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

This should disable directory indexing in all folders under /usr/tomcat/webapps/site, eg. /usr/tomcat/webapps/site/images/, /usr/tomcat/webapps/site/fubar/and so on.

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

回答by MrWhite

Options -Indexes FollowSymLinks
Options -Indexes FollowSymLinks

From the Apache 2.0 and Apache 2.2 docs:

来自 Apache 2.0 和Apache 2.2 文档

Warning
Mixing Optionswith a + or - with those without is not valid syntax, and is likely to cause unexpected results.

警告
Options+ 或 - 与没有的混合使用是无效的语法,并且可能会导致意外结果。

In Apache 2.4this will be...

Apache 2.4 中,这将是...

...rejected during server startup by the syntax check with an abort.

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

So, you basically need a +in front of FollowSymLinks(or remove the -Indexesargument altogether if you want to overrideall previously defined options). For example:

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

Options -Indexes +FollowSymLinks