nginx在访问目录时服务403错误


nginx serves 403 error when accessing directories

我的服务器大致有这样的树形结构:

www
 |-index.php
 |foo
   |-index.php
   |-bar.php

我可以访问/index.php,但访问//foo或任何其他内容会导致403。我尝试过各种配置,但都不起作用。

我的nginx.conf文件:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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/x-javascript text/xml application/xml application/xml+rss text/javascript;
    server {
        listen         80;
        server_name    localhost;
        root /var/www/html;
        location / {
            root      /var/www/html;
            #try_files $uri /index.php$is_args$args;
        }
        location ~ '.php$ {
            fastcgi_pass   web_fpm:9000;
            fastcgi_index index.php                                                                                                             
            fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; 
            include        fastcgi_params;
        }
    }
}

如果我取消注释注释行,那么任何请求都会服务于/index.php,所以这是不好的。

我找到了解决方案:设置索引。以下是片段:

server {
    listen         80;
    server_name    localhost;
    root /var/www/html;
    autoindex on;
    location / {
        index index.php index.html;
    }
    location ~ '.php$ {
        fastcgi_pass   web_fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
    }
}