PHP(Apache)静默地将HTTP 429和其他转换为500 [英] PHP (Apache) silently converting HTTP 429 and others to 500

查看:555
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆