本文介绍了PHP (Apache) 默默地将 HTTP 429 和其他转换为 500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 PHP 的 header() 方法中发现了一个奇怪的地方,它默默地将我的一些状态转换为 500.由于我没有在各种网络搜索中找到提及此行为的运气,因此我将其添加到此处以希望节省其他人有些恼怒,但也询问是否有人发现了比我想出的更好的解决方法(使用 PHP 或 Zend1).

I just discovered an oddity in PHP's header() method silently converting some of my statuses to 500. Since I had no luck in finding mention of this behavior in various web searches, I'm adding this here in the hope of saving others some aggravation, but also to ask if anyone has discovered a better workaround (either with PHP or Zend1) than I've come up with.

给定一个简单的 PHP 脚本,例如:

Given a simple PHP script like:

<?php
header('HTTP/1.1 429');
echo "Too Many Requests\n";

我希望收到类似的信息:

I would expect to receive something like:

HTTP/1.1 429
Date: Thu, 18 Jul 2013 22:19:45 GMT
Content-Length: 11
Content-Type: text/html; charset=UTF-8

Too Many Requests

相反,它实际上返回:

HTTP/1.1 500 Internal Server Error
Date: Thu, 18 Jul 2013 22:19:45 GMT
Content-Length: 11
Content-Type: text/html; charset=UTF-8

Too Many Requests

更神秘的是,我的 apache 错误日志中没有事件,并且访问日志显示了正确的状态代码(因此与发送到浏览器的不同):

Adding to the mystery, there are no events in my apache error log, and the access log shows the correct status code (thus different from what got sent to the browser):

$IP - - [18/Jul/2013:16:31:34 -0700] "GET /test/429.php HTTP/1.1" 429 11 "-" "curl/7.30.0"

在使用 401、420、426 等许多其他状态代码进行测试时,一切正常.

Everything works fine when testing with many other status codes like 401, 420, 426.

如果我明确发送标头('HTTP/1.1 429 Too Many Requests');这将是一个有用的解决方法,除了我使用 Zend Framework 并且它的 setHttpResponseCode 方法需要一个整数,它用作 php 的 header() 函数的第三个参数.

Everything also works fine if I am explicit and send header('HTTP/1.1 429 Too Many Requests'); This would be a useful workaround except that I'm using Zend Framework and its setHttpResponseCode method expects an integer, which it uses as the third parameter to php's header() function.

此后我发现它似乎特别适用于 RFC 6585 中添加的状态(请参阅 https://github.com/php/php-src/pull/274),虽然我有点困惑,为什么像 426 这样的状态在 5.4 的源代码中显然不存在时仍然有效.14 和 5.4.16(我测试过的两个版本),但像 429 这样的非功能性版本是.

I've since discovered that it seems to apply specifically to statuses added in RFC 6585 (see https://github.com/php/php-src/pull/274), though I'm a little confused why statuses like 426 work when they're clearly not present in the source code for 5.4.14 and 5.4.16 (the two versions I've tested against) but non-functional ones like 429 are.

更新:

正如答案所指出的,这主要是 Apache 问题,而不是 PHP,我已经相应地更新了标题.最有趣的似乎是这仅在某些版本的 Apache 中得到修复(新旧之间没有明显的一致性).我相信有问题的上游问题在这里:https://issues.apache.org/bugzilla/show_bug.cgi?id=44995

As answers have indicated, this is mostly an Apache issue, not PHP, I've updated the title accordingly. Most interesting seems to be that this is fixed only in certain versions of Apache (with no apparent consistency between old and new). I believe the upstream issue in question is here: https://issues.apache.org/bugzilla/show_bug.cgi?id=44995

推荐答案

它是 Apache,99% 肯定,我无法直接在它的文档中找到它,但我可以从下面的测试中推断它(Apache 版本 2.2.22)

It's Apache, 99% sure, I can't find it direcly in it's docs, but I can infer it from the test below (Apache version 2.2.22)

在您的配置中添加:

ErrorDocument 429 Aaargh to heavy

重启:

$ sudo /etc/init.d/apache2 restart
Syntax error on line 6 of /etc/apache2/conf.d/localized-error-pages:
Unsupported HTTP response code 429
Action 'configtest' failed.
The Apache error log may have more information.
   ...fail!

429似乎是最近在 rfc6585 中添加的,状态:提议,日期:2012 年 4 月.HTTP RFC 的一岁是......根据我的经验,这只是一个婴儿.加上在 Apache 中获取它的过程,然后在你的包存储库中......好吧,你可以尝试 Apache 2.4......

429 also seems a recent addition in rfc6585, status: proposed, date: April 2012. One year old for HTTP RFCs is... just a baby in my experience. Add to that the process of getting it in Apache, and then in your package repositories... Well, you could try Apache 2.4...

这篇关于PHP (Apache) 默默地将 HTTP 429 和其他转换为 500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:46