403 Forbidden, Index of / catch 22

403 Forbidden, Index of / catch 22

我在这个问题上真的很艰难,并且尝试了很多对某些人有效但对我无效的解决方案。首先是我的设置: Ubuntu 14.04.2 阿帕奇 2.4.7

我目前正在将我的网站迁移到 wordpress 文件夹结构 /var/www/dev 中,该文件夹结构设置为虚拟主机 - dev.conf 看起来像:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    ServerAdmin ubuntu@12.345.678.90
    DocumentRoot /var/www/dev
    ServerName dev.mysite.com

    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </Directory>
    <Directory /var/www/dev/>
            Options -Indexes +FollowSymLinks
            AllowOverride All
            #Require all granted
            Order allow,deny
            allow from all
    </Directory>
</VirtualHost>

我的问题是,当我第一次在浏览器中访问我的主页时 dev.mysite.com 它会将我定向到一个 403 页面:

Forbidden 
You don't have permission to access / on this server.

我的根文件夹中还有一个 .htaccess 文件,目前看起来像这样:

DirectoryIndex index.php index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

到目前为止,我遵循了 2 套不同的建议 - 一种说法是将我的配置选项更改为:

Options +Indexes

然而,当我导航到我的主页时,它为我提供了物理文件夹 "Index of /",其中包含我所有文件和文件夹的列表 - 我不想要。所以我将其保留为

Options -Indexes

另一组建议将其添加到 htaccess 文件中:

DirectoryIndex index.php index.html

我还检查了我的文件夹权限,全部归我的 apache 用户管理员所有 ubuntu:ubuntu 并且都遵循标准的 wordpress 文件夹权限,例如所有文件夹都是 755 所有文件都是 644(粗略的经验法则)。

我也尝试过使用新的 apache 2.4 标准

Require all granted 

而不是

Order allow,deny
allow from all

每次我进行更改时,我也会重新启动服务器,所以没有像那样愚蠢的事情。我在上面的配置中做了什么不让我加载我的网站?

特别是我的主页进入 403 禁止访问,任何其他页面,例如dev.mysite.com/features 加载此错误消息:

User-agent: *
Disallow: /wp-admin/

编辑: 上述问题仅在最初加载 url 时出现 - 如果我刷新页面,它会按预期加载。

这是我的文件所有权结构 - 也许这可能有用:

-rw-rw-r--  1 ubuntu   ubuntu       350 Sep 26 19:35 .htaccess
-rw-rw-r--  1 ubuntu   ubuntu      4951 Sep  9 13:21 wp-activate.php
drwxrwxr-x  9 ubuntu   ubuntu      4096 Sep  9 13:21 wp-admin
-rw-rw-r--  1 ubuntu   ubuntu       271 Sep  9 13:21 wp-blog-header.php
-rw-rw-r--  1 ubuntu   ubuntu      5007 Sep  9 13:21 wp-comments-post.php
-rw-rw-r--  1 ubuntu   ubuntu      3130 Sep 24 10:32 wp-config.php
-rw-rw-r--  1 ubuntu   ubuntu      2764 Sep 16 17:23 wp-config-sample.php
drwxrwxr-x  6 ubuntu   ubuntu      4096 Sep 24 10:46 wp-content
-rw-rw-r--  1 ubuntu   ubuntu      3286 Sep 16 17:23 wp-cron.php
drwxrwxr-x 12 ubuntu   ubuntu      4096 Sep  9 13:21 wp-includes
-rw-rw-r--  1 ubuntu   ubuntu      2380 Sep  9 13:21 wp-links-opml.php
-rw-rw-r--  1 ubuntu   ubuntu      3123 Sep  9 13:21 wp-load.php
-rw-rw-r--  1 ubuntu   ubuntu     34669 Sep 16 17:23 wp-login.php
-rw-rw-r--  1 ubuntu   ubuntu      8252 Sep  9 13:21 wp-mail.php
-rw-rw-r--  1 ubuntu   ubuntu     11062 Sep 16 17:23 wp-settings.php
-rw-rw-r--  1 ubuntu   ubuntu     25124 Sep 16 17:23 wp-signup.php
-rw-rw-r--  1 ubuntu   ubuntu      4035 Sep  9 13:21 wp-trackback.php
-rw-rw-r--  1 ubuntu   ubuntu      3055 Sep 16 17:23 xmlrpc.php

Forbidden 403 只是表示权限有问题。当服务器试图访问所请求的资源时,由于权限问题而受到限制,因此那些建议修改 Indexes 的人甚至都没有关闭,因为 Indexes 选项是控制目录列表的。以下是关于 Indexes option from Apache

的摘录

If a URL which maps to a directory is requested and there is no DirectoryIndex (e.g., index.html) in that directory, then mod_autoindex will return a formatted listing of the directory.

您的问题的根本原因显然是 "permissions"。

现在,对于解决方案部分 - 因为我不知道你的 .htaccessdev.conf 的所有内容,所以下面是我的逐步方法:

  1. 试试下面的代码片段。我正在尝试禁用您的 .htaccess 文件效果,看看这是否是根本原因。如果它有效,那么您检查您的 .htaccess 的所有内容是否存在可能的问题。

    <Directory /var/www/dev/>
            Options -Indexes +FollowSymLinks
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>
    
  2. 试试下面的代码片段。 Directory 的默认 Apache 访问权限是 Allow from All,所以我将其删除并让默认生效并且不进行排序。无论如何你也在尝试同样的事情..

    <Directory /var/www/dev/>
            Options -Indexes +FollowSymLinks
            AllowOverride All
    </Directory>
    

附带说明一下,我建议首先尝试对您的 Web 应用程序目录和文件执行 chmod 777,因为在大多数情况下权限问题是因为 UNIX 权限而不是 Apache 权限。


后续编辑:
为了进一步调试,我们将需要服务器错误信息,您能否启用最大调试并从 error.logaccess.log 获取内容(如果 HTTPS 访问则从 ssl 日志)。 Reference

此外,能否请您提供 DirectoryIndex 指令和您正在使用的 URL 的上下文。

很可能是您有权限问题,可能是因为 Apache 或 UNIX。

所以,让我们首先全面排除 UNIX 权限问题,所以暂时为您的 Web 应用程序资源尝试 chmod 777,如果有效,我们知道这是 UNIX 权限问题,我们将看看下一步该怎么做。

我对您 .htaccess 文件中的 RewriteBase /RewriteRule . /index.php [L] 持怀疑态度。
尝试 RewriteBase /var/www/dev 或您认为更相关但不是 /
的任何内容 此外,尝试使用 RewriteRule,因此请尝试使用 index.php 的绝对路径,因此请使用 RewriteRule . /var/www/dev/<<XYZ>>/index.php

尝试使用下面的 exact,如果您没有包含 mod_cgi 模块,那么您可能需要。

<Directory "/home/domain/www">
        Options +Indexes FollowSymLinks +ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
</Directory>


最终解决方案:

对于那些正在寻找解决方案的人 - 一旦 OP 使用 chmod -R www:data 递归授予权限,他就可以解决这个问题。所以,总而言之——这个问题不是因为 Apache 配置,而是因为 UNIX 权限。阅读下面 OP 的评论了解更多详情。