首页 文章

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

提问于
浏览
130

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

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

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

我看了Nginx 403 forbidden for all files但这没有帮助 .

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

15 回答

  • 6

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

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

    删除它,它应该工作:

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

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

  • 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/storagenote) . 使世界可写的东西是安全性差的 .

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

  • 3

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

    location /somedir {
           autoindex on;
    }
    
  • 2

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

    以下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用于该目录下的文件):
    (I am not sure, if this additional step is really required, just above 3 steps might be enough):

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

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

  • 0

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

    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
    

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

  • 2

    如果您只是尝试列出目录内容,请使用 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;
            }
    }
    
  • 6

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

    ps -ef|grep nginx
    
    ps aux|grep nginx|grep -v grep
    

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

    • 检查文件夹的访问状态

    ls -alt

    • 与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而group是nobody . 如果我们没有注意到这个用户和组,将引入403 .

  • 17

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

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

    这是我当时配置的一部分

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

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

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

    因为您正在使用 php-fpm ,所以应确保 php-fpm user与 nginx user相同 .

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

    php-fpm 用户需要写入权限 .

  • 56

    它看起来像是一些权限问题 .

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

    默认情况下,文件权限应为644和dirs 755.另请检查运行nginx的用户是否具有读取权限文件和目录 .

  • 1

    更改 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;
    }
    
  • 94

    您需要对静态文件目录具有执行权限 . 它们也需要由你的nginx用户和组来完成 .

  • 2
    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;
    

    解决了我的问题 .

  • 50
    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;
        }
    }
    }
    
  • 6

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

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

相关问题