【问题标题】:Nginx 403 error: directory index of [folder] is forbiddenNginx 403错误:[文件夹]的目录索引被禁止
【发布时间】:2013-10-17 14:21:58
【问题描述】:

我有 3 个域名,我正在尝试使用 Nginx 在一台服务器(Digital Ocean droplet)上托管所有 3 个站点。

mysite1.name mysite2.name mysite3.name

其中只有 1 个有效。其他两个导致 403 错误(以相同的方式)。

在我的 nginx 错误日志中,我看到:[error] 13108#0: *1 directory index of "/usr/share/nginx/mysite2.name/live/" is forbidden

我启用站点的配置是:

server {
        server_name www.mysite2.name;
        return 301 $scheme://mysite2.name$request_uri;
}
server {
        server_name     mysite2.name;

        root /usr/share/nginx/mysite2.name/live/;
        index index.html index.htm index.php;

        location / {
                try_files $uri $uri/ /index.html index.php;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

所有 3 个站点都有几乎相同的配置文件。

每个站点的文件都位于 /usr/share/nginx/mysite1.name/someFolder 等文件夹中,然后 /usr/share/nginx/mysite1.name/live 是指向该文件夹的符号链接。 (mysite2 和 mysite3 相同。)

我查看了Nginx 403 forbidden for all files,但这并没有帮助。

有什么想法可能是错的吗?

【问题讨论】:

  • 我认为您有 index.html index.php 文件丢失,您确定它们存在于该文件夹中吗?
  • 哦,你是对的;两个不工作的站点是一个 Laravel 项目(在 /public 子文件夹中有 index.php)和一个旧的 CodeIgniter 项目(在 /public_web 子文件夹中有 index.php)。但我不确定如何更改我的配置以使网站正常运行。
  • 就像@MohammadAbuShady 说的,我的文件夹中没有索引文件,所以出现了这个错误。
  • 我又遇到了这个错误,但这次的问题是我不小心将root 设置为/Users/myUsername/code/app 而不是/Users/myUsername/code/app/public
  • 这是服务器管理员大放异彩的时候。详情

标签: configuration nginx http-status-code-403 domain-name


【解决方案1】:

如果你关闭了目录索引,并且遇到了这个问题,可能是因为你使用的 try_files 有一个目录选项:

location / {
  try_files $uri $uri/ /index.html index.php;
}                 ^ that is the issue

删除它,它应该可以工作:

location / {
  try_files $uri /index.html index.php;
} 

为什么会这样

TL;DR:这是因为 nginx 会尝试索引目录,并被自己阻止。抛出OP提到的错误。

try_files $uri $uri/ 表示从根目录尝试uri 指向的文件,如果不存在,请尝试使用目录(因此是/)。当 nginx 访问一个目录时,它会尝试对其进行索引并将其中的文件列表返回给浏览器/客户端,但是默认情况下目录索引是禁用的,因此它返回错误“Nginx 403 error: directory index of [folder]被禁止”。

目录索引由autoindex 选项控制:https://nginx.org/en/docs/http/ngx_http_autoindex_module.html

【讨论】:

  • 这正是我遇到的问题。我不明白为什么try_files 没有尝试index.php,我只是不断得到403,“禁止...的目录索引”
  • @JCM,您介意添加一个关于为什么$uri/ 产生问题的解释吗?
  • 也解决了我的问题
  • 我遇到了同样的错误。我有 2 个站点,都在一个子域中。删除 $uri/ 就可以了。谢谢!
  • @luminol try_files $uri $uri/ 表示,从 Web 根目录尝试 uri 指向的文件,如果不存在,请尝试使用目录(因此是 /)。当 nginx 访问一个目录时,它会尝试对其进行索引并将其中的文件列表返回给浏览器/客户端,但是默认情况下目录索引是禁用的,因此它返回错误“Nginx 403 error: directory index of [folder]禁止”。目录索引由autoindex 选项控制:nginx.org/en/docs/http/ngx_http_autoindex_module.html
【解决方案2】:

这是有效的配置:

server {
    server_name www.mysite2.name;
    return 301 $scheme://mysite2.name$request_uri;
}
server {
    #This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf
    server_name mysite2.name;

     # The location of our project's public directory.
    root /usr/share/nginx/mysite2/live/public/;

     # Point index to the Laravel front controller.
    index           index.php;

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

然后浏览器中唯一的输出是 Laravel 错误:“哎呀,好像出了点问题。”

不要运行chmod -R 777 app/storage (note)。使某些东西在世界范围内可写是不好的安全性。

chmod -R 755 app/storage 有效且更安全。

【讨论】:

  • 是的,你们是对的;这是个坏主意。我会更新我的答案。人们也可能受益于stackoverflow.com/a/11996645/470749
  • 您还可以选择将文件夹组更改为 nginx 组,即 debian 上的 www-data。然后对文件夹设置更严格的权限,例如:chmod -R 640 app/storage 然后chown -R :www-data app/storage。这样,文件仅对应用程序所有者和 Web 服务器可见。而且根本没有人可以直接执行任何存储的(可能上传的)文件。 Nginx 应该只需要读取权限即可访问文件。
  • 自我注意:我刚刚得到这个 Nginx 403 再次再次问题是我不小心在@987654330 上忘记了public/ @。添加public/ 并运行service nginx restart 后,它工作了。
  • windows 呢?
【解决方案3】:

如果您只是想列出目录内容,请使用autoindex on;,例如:

location /somedir {
       autoindex on;
}

server {
        listen   80;
        server_name  domain.com www.domain.com;
        access_log  /var/...........................;
        root   /path/to/root;
        location / {
                index  index.php index.html index.htm;
        }
        location /somedir {
               autoindex on;
        }
}

【讨论】:

  • 我绝对不要autoindex on;向公众公开我的目录内容是个坏主意。
  • @Ryan 它总是归结为“你想做什么?”
  • 很明显他想删除 403 错误并让网页显示不显示整个目录内容(尤其是上面的讨论)
  • FWIW 我来这个问题是为了寻找自动索引选项,因为它是谷歌上错误的第一个结果。
【解决方案4】:

我遇到了类似的错误
--- 网页中的“403 Forbidden”
--- /var/log/nginx/error.log 错误日志中的“13: Permission denied”

以下 3 个步骤对我有用:

1:打开终端,看到如下内容

user1@comp1:/home/www/

所以,我的用户名是“user1”(来自上面)

2:在 /etc/nginx/nginx.conf 中更改了用户

# user www-data;
user user1;

3:重新加载nginx

sudo nginx -s reload  

此外,我已应用文件/文件夹权限(在我执行上述 3 个步骤之前)
(755 到我的目录,比如 /dir1/)&(644 用于该目录下的文件):
(我不确定,如果真的需要这个额外的步骤,上面 3 个步骤可能就足够了):

chmod 755 ./dir1/
chmod 644 ./dir1/*.*

希望这有助于快速某人。祝你好运。

【讨论】:

  • 谢谢兄弟,我有同样的问题,这是因为权限。我设置了文件夹和文件权限,现在可以正常使用了。
  • 很高兴听到我的帮助。 (如果可能的话,在您已知的领域,在您的空闲时间帮助他人,不要期待任何回报)
【解决方案5】:

事实上,您需要检查几件事。 1.检查你的nginx的运行状态

ps -ef|grep nginx

ps aux|grep nginx|grep -v grep

这里我们需要检查谁在运行 nginx。请记住用户和组

  1. 查看文件夹的访问状态

    ls -alt

  2. 将文件夹的状态与nginx的比较

(1)如果文件夹的访问状态不正确

sudo chmod 755 /your_folder_path

(2)如果文件夹的用户和组与nginx运行的不同

sudo chown your_user_name:your_group_name /your_folder_path

并更改 nginx 的运行用户名和组

nginx -h

找到nginx配置文件在哪里

sudo vi /your_nginx_configuration_file

//in the file change its user and group
user your_user_name your_group_name;

//restart your nginx
sudo nginx -s reload

因为nginx默认运行的用户是nobody,组是nobody。如果我们没有注意到这个用户和组,就会引入 403。

【讨论】:

    【解决方案6】:

    我遇到了同样的问题,日志文件显示了这个错误:

    2016/03/30 14:35:51 [error] 11915#0: *3 directory index of "path_scripts/viewerjs/" is forbidden, client: IP.IP.IP.IP,     server: domain.com, request: "GET /scripts/viewerjs/ HTTP/1.1", host: "domain", referrer: "domain.com/new_project/do_update"
    

    我正在托管一个带有 codeignitor 框架的 PHP 应用程序。当我想查看上传的文件时,我收到了403 Error

    问题是,nginx.conf 没有正确定义。而不是

    index index.html index.htm index.php
    

    我只包括

    index index.php
    

    我的根目录中有一个 index.php,我认为这就足够了,我错了;) 提示给了我NginxLibrary

    【讨论】:

    • 谢谢伙计..我在同一条船上..我花了几个小时弄清楚为什么我的 wordpress 根本不起作用! nginx 主配置中需要 index 指令,以便我的 wordpress 安装工作include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; index index.html index.htm index.php;
    【解决方案7】:

    您可能会因为 Nginx 政策(例如“拒绝”)而收到此信息,或者您可能会因为 Nginx 配置错误而收到此信息,或者您可能会因为文件系统限制而收到此信息。

    您可以确定它是否是后者(并且可能通过使用 strace 来查看配置错误的证据(除了,OP 将无权访问):

    # pidof nginx
    11853 11852
    
    # strace -p 11853 -p 11852 -e trace=file -f
    Process 11853 attached - interrupt to quit
    Process 11852 attached - interrupt to quit
    [pid 11853] stat("/var/www/html/kibanaindex.html", 0x7ffe04e93000) = -1 ENOENT (No such file or directory)
    [pid 11853] stat("/var/www/html/kibana", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
    ^CProcess 11853 detached
    Process 11852 detached
    

    在这里,我正在检查 nginx 在运行测试时完成的文件系统活动(我遇到了和你一样的错误)。

    这是我当时配置的一部分

        location /kibana/3/ {
            alias /var/www/html/kibana;
            index index.html;
        }
    

    在我的情况下,正如 strace 清楚地表明的那样,将“别名”加入“索引”并不是我所期望的,而且似乎我需要养成总是在目录名称后面附加一个/,所以在我的情况下,以下工作:

        location /kibana/3/ {
            alias /var/www/html/kibana/;
            index index.html;
        }
    

    【讨论】:

    • 谢谢。我知道我没有权限问题,您的评论帮助我找到了解决方案。我在别名的末尾添加了一个“/”,它工作正常。
    • 你是我的英雄@Cameron Kerr,根据我的经验,问题是 nginx raise 403 for not found files on alias directory e.g /home/web/public。为什么 nginx 尝试访问这些未找到的文件是因为我忘记删除这一行 index index.html index.htm index.nginx-debian.html; 因为那些文件不在我的公共目录中。
    • 我以前从未听说过strace。它有助于解决这个问题(我犯了同样的错误,将/ 留在了alias 的末尾)并显示了/var/log/nginx/error.log 中未显示的错误...你能解释一下它是如何工作的吗?
    【解决方案8】:

    好像是权限问题。

    尝试像在 mysite1 中那样将所有权限设置为其他站点。

    默认情况下,文件权限应为 644 和 dirs 755。 还要检查运行 nginx 的用户是否有权读取该文件和目录。

    【讨论】:

      【解决方案9】:

      这是我设法在我的 Kali 机器上修复它的方法:

      • 定位到目录:

        cd /etc/nginx/sites-enabled/

      • 编辑“默认”配置文件:

        sudo nano default

      • location 块中添加以下行:

        location /yourdirectory {
          autoindex on;
          autoindex_exact_size off;
        }
        
      • 请注意,我已在特定目录中激活自动索引 仅限/yourdirectory。否则,它将为您计算机上的所有文件夹启用,而您不需要它。

      • 现在重启你的服务器,它现在应该可以工作了:

        sudo service nginx restart

      【讨论】:

        【解决方案10】:

        try_files 更改为指向index.php 路径,在您提到的“Laravel”中它应该是这样的

        location / {
            try_files $uri $uri/ /public/index.php$request_uri;
        }
        

        并在“codeigniter”项目中尝试这样

        location / {
            try_files $uri $uri/ /public_web/index.php$request_uri;
        }
        

        【讨论】:

          【解决方案11】:

          因为您使用的是php-fpm,所以您应该确保php-fpm 用户与nginx 用户相同。

          检查/etc/php-fpm.d/www.conf,如果不是,则将php用户和组设置为nginx

          php-fpm 用户需要写权限。

          【讨论】:

            【解决方案12】:

            您需要对静态文件目录具有执行权限。他们还需要由您的 nginx 用户和组 chown'ed。

            【讨论】:

            • 我认为他只需要对nginx进程的读取权限?
            【解决方案13】:
            location ~* \.php$ {
                ...
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
            }
            

            更改默认值

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            

            解决了我的问题。

            【讨论】:

              【解决方案14】:

              对我来说,问题是基本路线以外的任何路线都在工作,添加此行解决了我的问题:

              index           index.php;
              

              完整的东西:

              server {
              
                  server_name example.dev;
                  root /var/www/example/public;
                  index           index.php;
              
                  location / {
                      try_files $uri $uri/ /index.php?$query_string;
                  }
              
                  location ~ \.php$ {
                      include /etc/nginx/fastcgi_params;
                      fastcgi_pass  127.0.0.1:9000;
                      fastcgi_index index.php;
                      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                  }
              }
              

              【讨论】:

                【解决方案15】:

                在我的例子中,我使用 hhvm 监听端口 9000,而 nginx 配置中的 fastcgi_pass 行不正确。

                另外,如果您使用 mysql 并且从 hhvm 到数据库的连接不起作用,请检查您是否安装了 apparmor

                【讨论】:

                  【解决方案16】:
                  6833#0: *1 directory index of "/path/to/your/app" is forbidden, client: 127.0.0.1, server: lol.com, request: "GET / HTTP/1.1", host: "localhost"    
                  

                  我在运行 Ubuntu 15.10 时遇到了 403 Forbidden 错误,原因很简单。 在 nginx.conf(nginx 的配置文件)中,用户是“www-data”。 一旦我将用户名更改为 [我的用户名],假设我的用户名获得了必要的权限,它就可以正常工作。 我遵循的步骤:

                  chmod 755 /path/to/your/app    
                  

                  我的配置文件如下所示:

                  **user [my username]**;#I made the change here.
                  worker_processes auto;
                  pid /run/nginx.pid;
                  events {
                  worker_connections 768;
                  # multi_accept on;
                  }
                  
                  http {
                  
                  ##
                  # Basic Settings
                  ##
                  
                  sendfile on;
                  tcp_nopush on;
                  tcp_nodelay on;
                  keepalive_timeout 65;
                  types_hash_max_size 2048;
                  # server_tokens off;
                  
                  # server_names_hash_bucket_size 64;
                  # server_name_in_redirect off;
                  
                  include /etc/nginx/mime.types;
                  default_type application/octet-stream;
                  
                  ##
                  # SSL Settings
                  ##
                  
                  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
                  ssl_prefer_server_ciphers on;
                  
                  ##
                  # Logging Settings
                  ##
                  
                  access_log /var/log/nginx/access.log;
                  error_log /var/log/nginx/error.log;
                  
                  ##
                  # Gzip Settings
                  ##
                  
                  gzip on;
                  gzip_disable "msie6";
                  
                  # gzip_vary on;
                  # gzip_proxied any;
                  # gzip_comp_level 6;
                  # gzip_buffers 16 8k;
                  # gzip_http_version 1.1;
                  # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
                  
                  ##
                  # Virtual Host Configs
                  ##
                  
                  include /etc/nginx/conf.d/*.conf;
                  include /etc/nginx/sites-enabled/*;
                  
                  
                  server {
                      listen 80;
                  
                      server_name My_Server;
                  
                      access_log  /var/log/nginx/access.log;
                      error_log  /var/log/nginx/error.log;
                  
                      location / {
                          proxy_pass         http://127.0.0.1:8000;
                          proxy_redirect     off;
                  
                          proxy_set_header   Host             $host;
                          proxy_set_header   X-Real-IP        $remote_addr;
                          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                      }
                  }
                  }
                  

                  【讨论】:

                    【解决方案17】:

                    我解决了我的问题,如果我配置如下:

                    location = /login {
                        index  login2.html;
                    }
                    

                    它会显示 403 错误。

                    [error] 4212#2916: *2 directory index of "D:\path/to/login/" is forbidden
                    

                    我试过autoindex on,但没有用。 如果我像这样更改我的配置,它可以工作。

                    location = /login/ {
                        index  login2.html;
                    }
                    

                    我认为完全匹配,如果是路径应该是目录。

                    【讨论】:

                      【解决方案18】:
                      1. 检查目录中是否缺少 index.html 或 index.php

                      2. 查看位于 /var/log/nginx 的错误日志文件,然后打开

                        vim 错误日志

                      【讨论】:

                        【解决方案19】:

                        当你想保留目录选项时,你可以像这样把 index.php 放在 $uri 前面。

                        try_files /index.php $uri $uri/
                        

                        【讨论】:

                        • 有了这个,nginx在浏览器中下载文件
                        【解决方案20】:

                        就我而言,它与 CentOS 7 中的 SELinux 相关:

                        您可以运行以下命令检查它是否已启用:

                        cat /etc/selinux/config
                        

                        示例输出:

                        SELINUX=enforcing
                        SELINUXTYPE=targeted
                        

                        永久禁用 SELinux 编辑 /etc/selinux/config 文件,运行:

                        sudo vi /etc/selinux/config
                        

                        将 SELINUX 设置为禁用:

                        SELINUX=disabled
                        

                        在 vi/vim 中保存并关闭文件。重启Linux系统:

                        sudo reboot
                        

                        【讨论】:

                          【解决方案21】:

                          就我而言,我没有运行此命令

                          sudo apt-get install php7.4-fpm
                          

                          【讨论】:

                            猜你喜欢
                            • 2018-10-16
                            • 2016-06-19
                            • 2019-04-28
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2013-09-03
                            • 1970-01-01
                            • 2018-12-01
                            相关资源
                            最近更新 更多