1

I kept getting "directory forbidden error" on image directory /images/upload.

By adding autoindex on that directory, I can remove this error. But I don't want people to see what are under that directory.

How do I remove this error?

Nginx config

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

3 回答 3

1

返回索引页或 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 错误代码、使用自动索引或试图用虚假页面隐藏您的问题,您可以完全按照您的期望做 :)

于 2014-05-11T10:09:28.520 回答
1

您有以下设置:

index        index.php index.html index.htm;

这意味着当自动索引关闭时,您的网络服务器将对目录路径的每个请求:

  1. 在该目录中查找 index.php,如果找到则返回
  2. 如果未找到 index.php,它将查找 index.html 并返回
  3. 如果 index.html 也没有找到,它将返回 index.htm
  4. 如果没有找到,它将返回 403

因此,如果您想避免 403 错误页面,您需要在该目录中设置 index.php、index.html 或 index htm。

或者,您可以设置error_page 403 some_page.html; 返回看起来不同的 403 结果。

于 2013-03-15T08:51:23.720 回答
0

这将返回 403 目录禁止,没有错误,仅适用于http://example.com/

location = / {
    return 403;
}

所以,在你的情况下:

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

这将只为 /images/upload/ 目录返回 403 目录禁止,没有错误。

于 2016-04-21T01:11:36.963 回答