【问题标题】:Apache 406 Not Acceptable when specifying Accept: header指定 Accept: 标头时 Apache 406 Not Acceptable
【发布时间】:2013-03-21 03:11:29
【问题描述】:

我自己尝试找到解决方案,但其他 406 问题的解决方案对我没有帮助。

我在我的 Mac 上启用了 PHP 和 Apache Web 服务器。我发现如果我通过带有 Accept: 标头集的请求(POST 或 GET),那么它会失败并出现 406 错误:

$ curl -X GET -H "Accept: application/json" http://localhost/test/tester
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>406 Not Acceptable</title>
</head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /test/tester could not be found on this server.</p>
Available variants:
<ul>
<li><a href="tester.php">tester.php</a> , type application/x-httpd-php</li>
</ul>
</body></html>

但是,如果我排除“接受:应用程序/json”,则请求执行时不会出错。

我尝试添加这个 test/tester.var(在与 test/tester.php 相同的目录中):

URI: tester

Content-type: application/json
URI: tester.php

我的意图是通过执行 tester.php 来指示 Apache 处理带有“Accept: application/json”的请求。但这对我没有帮助(我还在我的 httpd.conf 文件中的“IfModule mime_module”下添加了“AddHandler type-map .var”并重新启动了服务器)。

'mod_security' 似乎没有配置,但我还是将以下 .htaccess 添加到了我的 test/ 目录:

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

这些是我在网上找到的解决方案,没有一个对我有用。

有什么想法吗?

【问题讨论】:

    标签: php apache


    【解决方案1】:

    我终于找到了解决办法!正如我所怀疑的,*mod_security* 与我的问题无关。

    以下是我对 /etc/apache2/httpd.conf 所做的更改

    1. 通过取消注释“AddHandler type-map var”行启用类型映射 在“IfModule mime_module”部分中。
    2. 在“IfModule mime_module”部分添加以下两行:

      AddType application/x-httpd-php .php
      MultiviewsMatch Handlers Filters
      

    在我的 PHP 源目录中,我在与 tester.php 相同的目录中创建了一个 tester.var,其内容为:

        Content-type: application/json
        URI: tester.php
    

    现在一切都好。

    【讨论】:

    • 是什么导致了问题本身?我在 Mac 中通过 Chrome 发出 ajax POST 请求时遇到了同样的问题,但在其他操作系统和网络浏览器上工作正常
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 2018-07-31
    • 2013-08-10
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多