Apache 2.4 中不允许 PUT 和 DELETE 请求

时间:2021-05-06 07:35:08

标签: apache

我正在尝试使用 PHP 在我的服务器上创建 RESTFul API。 GET 或 POST 请求没有问题,但我无法获得 PUT 或 DELETE 请求。

起初,我尝试不配置 Apache:我得到的只是 403 未经授权的错误。然后我使用限制:

<Limit GET HEAD POST PUT DELETE OPTIONS>
  Require all granted
</Limit>

我收到 405 Method Not Allowed 错误。

从这里开始,我尝试了多种配置(AllowMethods,...),但仍然收到 405 错误。 当我激活 Apache 日志的上层时,它表示一切正常,但仍然返回 405 代码:

[authz_core:debug] [pid 43259] mod_authz_core.c(817): AH01626: authorization result of Require all granted: granted
[authz_core:debug] [pid 43259] mod_authz_core.c(817): AH01626: authorization result of <RequireAny>: granted
[http:trace3] [pid 43259] http_filters.c(1125): Response sent with status 405, headers:
[http:trace5] [pid 43259] http_filters.c(1134):   Date: Thu, 06 May 2021 06:55:24 GMT
[http:trace5] [pid 43259] http_filters.c(1137):   Server: Apache/2.4.41 (Ubuntu)
[http:trace4] [pid 43259] http_filters.c(955):   Allow: GET,POST,OPTIONS,HEAD
[http:trace4] [pid 43259] http_filters.c(955):   Content-Length: 306
[http:trace4] [pid 43259] http_filters.c(955):   Content-Type: text/html; charset=iso-8859-1

我读到有时像 Jira 这样的服务器 Web 应用程序会导致这种情况,但我只安装了 PhpMyAdmin。

如何使用 PUT 或 DELETE 请求?

注意:Apache/2.4.41 (Ubuntu)

1 个答案:

答案 0 :(得分:0)

已修复。

全局 Apache2 配置 /etc/apache2/mods-enabled/userdir.conf 定义以下规则:

<Limit GET POST OPTIONS>
  Require all granted
</Limit>
<LimitExcept GET POST OPTIONS>
  Require all denied
</LimitExcept >

我不知道为什么 <Limit> 在 .htaccess 中被部分忽略(活动时错误 405 而不是 403),但如果没有 <Limit> 就可以工作。