apache - 缺少目录索引时的 404 与 403

标签 apache http http-status-code-404 http-status-codes http-status-code-403

这主要是关于解释 HTTP 规范的最佳方式的哲学问题。没有目录索引的目录(例如 index.html)应该返回 404 还是 403? (403 是 Apache 中的默认设置。)

例如,假设存在以下 URL 并且可以访问:

http://example.com/files/file_1/
http://example.com/files/file_2/

但是什么都没有:

http://example.com/files/

(假设我们使用 301 强制所有 URL 的尾部斜杠。)

我认为应该考虑几件事:

  • 在这种情况下,Apache 默认返回 403。这对我很重要。他们考虑过这个问题,并决定使用 403。
  • 根据 W3C,403 表示“服务器理解请求,但拒绝执行请求。”我认为这意味着如果 URL 有意义但仍然被禁止,您应该返回 403。
  • 如果客户端正确猜测 URL 映射到磁盘上的真实目录,403 可能会导致信息泄露。
  • http://example.com/files/ 不是资源,它在内部映射到目录的事实不应该与状态代码相关。
  • 如果您从客户端的角度将 URL 方案解释​​为定义目录结构,则内部实现仍然无关紧要,但也许外观确实对状态代码有一定影响。也许,即使您创建了相同的 URL 结构而不在内部使用目录,您仍然应该使用 403,因为它是关于客户对目录结构的看法。

权衡之下,您认为最好的方法是什么?我们是否应该只说“资源就是资源,如果不存在,就是 404”?或者我们应该说,“如果它有斜杠,它看起来像客户端的目录,因此如果没有索引它就是 403?”

如果您属于 403 阵营,您认为即使内部实现不使用目录,您是否应该特意返回 403?例如,假设您有一个带有此 URL 的动态 Web 应用程序:http://example.com/users/joe,它映射到为 Joe 生成个人资料页面的一些代码。假设您没有编写列出所有用户的内容,http://example.com/users/ 是否应该返回 403? (在这种情况下,即使不是全部,也有许多 Web 框架返回 404。)

最佳答案

回答这个问题的第一步是引用 RFC 2616: HTTP/1.1 .特别是讨论 403 Forbidden 的部分和 404 Not Found .

  • 10.4.4 403 Forbidden

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

  • 10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

我对此的解释是 404 是更一般的错误代码,它只是表示“那里什么都没有”。 403 表示“那里什么都没有,不要再试一次!”。

Apache 可能在没有明确索引文件的目录上返回 403 的一个原因是自动索引(即列出其中的所有文件)被禁用(也称为“禁止”)。在那种情况下,说“禁止列出该目录中的所有文件”比说“没有目录”更有意义。

关于apache - 缺少目录索引时的 404 与 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5075300/

相关文章:

java - 如何使用基于 Java 的注释配置的 Spring MVC 全局处理 404 异常

apache - 带有白屏和消息 "redirecting to..."的 laravel 重定向中断帖子

java - 谁能给我提供 Java 代码来生成 OAuth 请求中使用的时间戳?

django - 随机网址返回错误代码500,而不是404

java - 在 Spring 中处理 404 错误?

nc 收到时的 HTTP 错误响应

java - 忽略 Apache Olingo 中的字段

apache - 尝试显示没有 html 扩展名的 url 时奇怪的 apache 行为

android - 为什么在 Android 中执行此 HttpGet 会引发 NullPointerException?

sockets - 如何通过 tcp 套接字传输大数据