Error handling 在Nginx中,它必须是自动索引或目录禁止错误?

Error handling 在Nginx中,它必须是自动索引或目录禁止错误?,error-handling,nginx,Error Handling,Nginx,我一直在图像目录/images/upload上看到“目录禁止错误” 通过在该目录上添加自动索引,我可以删除此错误。但我不想让人们看到那个目录下有什么 如何删除此错误 Nginx配置 server { listen 80; server_name www.no-dev.com; index index.php index.html index.htm; root /opt/www/no_web; location ~ .*\.(ph

我一直在图像目录/images/upload上看到“目录禁止错误”

通过在该目录上添加自动索引,我可以删除此错误。但我不想让人们看到那个目录下有什么

如何删除此错误

Nginx配置

server
{
  listen       80;
  server_name  www.no-dev.com;
  index        index.php index.html index.htm;
  root         /opt/www/no_web;

  location ~ .*\.(php|php5)?$
  {
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include       fastcgi.conf;
  }

  #location /images {
  #  autoindex on;
  #}

  location ~* \.(css|js)$ {
              add_header  Last-Modified: $date_gmt;
              expires 1y;
              access_log off;
  }

  location ~* \.(jpg|jpeg|gif|png|ico|bmp|swf)$ {
              expires max;
              access_log off;
  }
  access_log logs/no-dev.log main;
}

您拥有以下集合:

index        index.php index.html index.htm;
这意味着当autoindex关闭时,您的Web服务器将为每个目录路径请求:

  • 在该目录中查找index.php,如果找到,将其返回
  • 如果找不到index.php,它将查找index.html并返回该值
  • 如果找不到index.html,它将返回index.htm
  • 如果没有找到,它也会返回403
  • 因此,如果要避免403错误页面,需要在该目录中设置index.php、index.html或index.htm


    或者,您可以设置
    error\u page 403 some\u page.html
    返回看起来不同的403结果。

    返回索引页或403错误不是目录的唯一选择。例如,有时404错误更合适。尤其是当您想隐藏文件夹时

    在这种情况下,有一种优雅的方式可以让您随心所欲:

    # Create a fictive location which does what you want (404 here) 
    location /404/ {
        internal;
        return 404;
    }
    
    
    # Use this fictive location as a folder index. That's all.
    index /404/;
    
    目录索引请求将由(虚拟的)/404/”内部位置处理,并将导致404错误。请注意,这是一个内部位置客户端浏览器不会以任何方式重定向。它只是得到了一个404返回码

    这里,404错误只是一个例子。当你重定向到一个内部位置时,你可以做任何你想做的事情


    因此,您不必返回403错误代码,也不必使用自动索引或试图隐藏虚假页面的问题,而是可以完全按照您的预期操作:)

    这将返回403目录禁止,不会出现错误,仅用于:

    因此,在你的情况下:

    location = /images/upload/ {
        return 403;
    }
    

    这将返回403目录禁止,没有错误,仅用于/images/upload/目录。

    那么您想做什么?当人们浏览到
    /images/uploads
    时,你希望看到什么行为?好问题。根据cobaco的回答,我可以使用错误页面而不是自动索引。不,@cobaco的回答并没有涵盖所有的可能性,只有一个。这个问题的答案是:不。自动索引、索引和403错误不是唯一的可能性。你可以做你想做的事。看看我刚才的回答。虽然我设置了403错误页面,但当我尝试访问/images/upload时,我仍然在错误日志中得到“directory-forbidden”。有没有办法忽略这一点?不要让它写错误日志?@cobaco的意思是:只需在
    /images/upload/
    中创建一个空index.php文件。e、 g.
    touch/images/upload/index.php
    。任何未来的请求都只会加载该空页面。
    location = /images/upload/ {
        return 403;
    }