繁体   English   中英

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

[英]Nginx 403 error: directory index of [folder] is forbidden

我有 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,但这没有帮助。

关于什么可能是错的任何想法?

如果您关闭了目录索引,并且遇到了这个问题,可能是因为您使用的 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

这是有效的配置:

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)。 使某些东西在世界范围内可写是不好的安全性。

chmod -R 755 app/storage工作并且更安全。

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

location /somedir {
       autoindex on;
}

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

我遇到了类似的错误
--- 网页中的“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/*.*

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

实际上,您需要检查几件事。 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。

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

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

你可能会因为 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;
    }

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

  • 定位到目录:

    cd /etc/nginx/sites-enabled/

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

    sudo nano default

  • location块中添加以下行:

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

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

    sudo service nginx restart

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

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

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

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

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

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

php-fpm用户需要写权限。

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

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

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

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;

解决了我的问题。

在我的情况下,我使用hhvm监听端口 9000 并且 nginx 配置中的fastcgi_pass行不正确。

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

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

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

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

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

  1. 检查目录中是否缺少 index.html 或 index.php

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

    vim 错误日志

就我而言,它与 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

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

try_files /index.php $uri $uri/

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

sudo apt-get install php7.4-fpm
location / {
root  fileLocation;
index  index.html index.htm;
expires 0;
if_modified_since off;}

在这里,我使用了alias而不是root ,这导致了我的错误。 这种配置工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM