简体   繁体   English

指定Accept:标头时,Apache 406不可接受

[英]Apache 406 Not Acceptable when specifying Accept: header

I've tried to find a solution for this myself but solutions to other 406 problems haven't helped me. 我试图自己找到一种解决方案,但其他406个问题的解决方案却无济于事。

I have enabled PHP and the Apache web server on my Mac. 我已经在Mac上启用了PHP和Apache Web服务器。 I have found that if I pass a request (POST or GET) with the Accept: header set, then it fails with a 406 error: 我发现如果我通过设置了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>

However, if I exclude the "Accept: application/json" the request executes without error. 但是,如果我排除“ Accept:application / json”,则请求将执行而不会出错。

I tried adding this test/tester.var (in the same dir as test/tester.php): 我尝试添加此test / tester.var(与test / tester.php相同的目录):

URI: tester

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

My intention was to direct Apache to handle request with "Accept: application/json" by executing tester.php. 我的意图是通过执行tester.php来指示Apache使用“ Accept:application / json”来处理请求。 But it hasn't helped me (I also added 'AddHandler type-map .var' under 'IfModule mime_module' inside my httpd.conf file and restarted the server). 但这并没有帮助我(我还在httpd.conf文件中的“ IfModule mime_module”下添加了“ AddHandler type-map .var”,然后重新启动了服务器)。

'mod_security' doesn't appear to be configured, but I added the following .htaccess to my test/ directory anyways: 'mod_security'似乎没有配置,但是无论如何我还是将以下.htaccess添加到了test /目录:

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

These are solutions that I found on the net by none have worked for me. 这些是我在网上找不到的解决方案,对我没有用。

Any ideas? 有任何想法吗?

I finally figured out a fix! 我终于想出了解决办法! As I suspected, *mod_security* had nothing to do with my problem. 正如我所怀疑的那样,* mod_security *与我的问题无关。

Here are the changes that I made to /etc/apache2/httpd.conf : 这是我对/etc/apache2/httpd.conf所做的更改:

  1. Enabled type maps in by uncommenting "AddHandler type-map var" line in the "IfModule mime_module" section. 通过取消注释“ IfModule mime_module”部分中的“ AddHandler type-map var”行,可以启用已启用的类型映射。
  2. Added the following two lines to the "IfModule mime_module" section: 在“ IfModule mime_module”部分中添加了以下两行:

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

In my PHP source directory, I created a tester.var in the same directory as tester.php with the contents: 在我的PHP源目录中,我在与tester.php相同的目录中创建了一个tester.var ,其内容为:

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

It's all good now. 现在一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM