Nginx 403错误:禁止[文件夹]的目录索引

Rya*_*yan 158 configuration nginx domain-name http-status-code-403

我有3个域名,我正在尝试使用Nginx在一台服务器(数字海洋液滴)上托管所有3个站点.

mysite1.name mysite2.name mysite3.name

其中只有一个有效.另外两个导致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;
        }
}
Run Code Online (Sandbox Code Playgroud)

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

每个站点的文件都在/usr/share/nginx/mysite1.name/someFolder等文件夹中,然后/usr/share/nginx/mysite1.name/live是一个符号链接.(与mysite2和mysite3相同.)

我看过禁止所有文件的Nginx 403,但这没有用.

关于可能出错的任何想法?

JCM*_*JCM 135

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

location / {
  try_files $uri $uri/ /index.html index.php;
}                 ^ that is the issue
Run Code Online (Sandbox Code Playgroud)

删除它,它应该工作:

location / {
  try_files $uri /index.html index.php;
} 
Run Code Online (Sandbox Code Playgroud)

从我所看到的,这是因为nginx将尝试索引目录,并被自己阻止.抛出OP提到的错误.

  • @JCM,您介意为_why_添加$ uri /`造成问题吗? (2认同)
  • @luminol`try_files $ uri $ uri /`表示从Web根目录尝试uri指向的文件,如果该文件不存在,则尝试使用目录(因此使用`/`)。当nginx访问目录时,它将尝试为其建立索引并将其中的文件列表返回到浏览器/客户端,但是默认情况下,目录索引处于禁用状态,因此它将返回错误“ Nginx 403错误:[文件夹]的目录索引”是禁止的”。目录索引由`autoindex`选项控制:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html (2认同)

Rya*_*yan 62

这是有效的配置:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后浏览器中唯一的输出是Laravel错误:"糟糕,看起来出了问题."

不要跑chmod -R 777 app/storage(注意).使世界可写的东西是安全性差的.

chmod -R 755 app/storage 工作,更安全.

  • 永远不要运行`chmod -R 777`来让事情顺利进行! (156认同)
  • 自我注意:我刚刚再次得到这个Nginx 403*而且*再次*问题是我在"root/usr/share/nginx/mysitename/public /;"中意外地离开了`public /`.添加`public /`并运行`service nginx restart`后,它就可以了. (3认同)

maz*_*maz 55

如果您只是尝试列出目录内容,请使用autoindex on;如下:

location /somedir {
       autoindex on;
}
Run Code Online (Sandbox Code Playgroud)

  • 很明显,他想删除403错误并让网页显示不显示整个目录内容(特别是上面的讨论) (12认同)
  • 我绝对不希望`autoindex on`; 将我的目录内容公开给公众是一个坏主意. (8认同)
  • @Ryan它总是归结为"你想做什么?" (5认同)
  • FWIW 我来到这个问题寻找 autoindex 选项,因为它是 Google 上错误的第一个结果。 (3认同)

Man*_*ddy 19

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

以下3个步骤对我有用:

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

user1@comp1:/home/www/
Run Code Online (Sandbox Code Playgroud)

所以,我的用户名是"user1"(从上面)

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

# user www-data;
user user1;
Run Code Online (Sandbox Code Playgroud)

3:重新加载nginx

sudo nginx -s reload  
Run Code Online (Sandbox Code Playgroud)

另外,我已经应用了文件/文件夹权限(在我执行上述3个步骤之前)
(755到我的目录,比如说/ dir1 /)&(644用于该目录下的文件):(
我不确定,如果这个额外的步骤是真的需要,只需3步以上即可)

chmod 755 ./dir1/
chmod 644 ./dir1/*.*
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助快速的人.祝你好运.

  • 谢谢兄弟,我也遇到了同样的问题,是因为权限问题。我设置了文件夹和文件权限,现在工作正常。 (2认同)
  • 很高兴听到我很有帮助.(如果可能的话,在您的已知域中帮助其他人,在您的空闲时间内,不要期待任何事情) (2认同)

RAZ*_*229 10

以下是我在 Kali 机器上修复该问题的方法:

  • 定位到目录:

    cd /etc/nginx/sites-enabled/

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

    sudo nano default

  • 在块中添加以下行location

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

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

    sudo service nginx restart


Hai*_*mei 8

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

ps -ef|grep nginx

ps aux|grep nginx|grep -v grep
Run Code Online (Sandbox Code Playgroud)

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

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

    ls -alt

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

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

sudo chmod 755 /your_folder_path
Run Code Online (Sandbox Code Playgroud)

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

sudo chown your_user_name:your_group_name /your_folder_path
Run Code Online (Sandbox Code Playgroud)

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

nginx -h
Run Code Online (Sandbox Code Playgroud)

找到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
Run Code Online (Sandbox Code Playgroud)

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


小智 7

看起来像是一些权限问题。

尝试将您在 mysite1 中所做的所有权限设置为其他站点。

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


the*_*ter 7

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

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"
Run Code Online (Sandbox Code Playgroud)

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

问题是,nginx.conf 没有正确定义.代替

index index.html index.htm index.php
Run Code Online (Sandbox Code Playgroud)

我只包括在内

index index.php
Run Code Online (Sandbox Code Playgroud)

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


Cam*_*err 6

你可能因为Nginx策略(例如"deny")而得到这个,或者你可能因为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
Run Code Online (Sandbox Code Playgroud)

在这里我正在检查nginx在运行测试时完成的文件系统活动(我和你有同样的错误).

这是我当时配置的一部分

    location /kibana/3/ {
        alias /var/www/html/kibana;
        index index.html;
    }
Run Code Online (Sandbox Code Playgroud)

在我的情况下,正如strace非常清楚地表明的那样,"别名"与"索引"的加入并不是我所期望的,似乎我需要养成总是用/附加目录名的习惯,所以在我的情况下,以下工作:

    location /kibana/3/ {
        alias /var/www/html/kibana/;
        index index.html;
    }
Run Code Online (Sandbox Code Playgroud)


zer*_*nes 6

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

index           index.php;
Run Code Online (Sandbox Code Playgroud)

完整的东西:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*ady 5

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

location / {
    try_files $uri $uri/ /public/index.php$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

在“codeigniter”项目中尝试这样

location / {
    try_files $uri $uri/ /public_web/index.php$request_uri;
}
Run Code Online (Sandbox Code Playgroud)