【问题标题】:406 Server Error406 服务器错误
【发布时间】:2012-08-23 03:19:28
【问题描述】:

我有一个网站,可以在我们的本地机器和我们的测试服务器上正常运行。但是,当我们将它放在实时服务器上时,网站会随机出现 406 错误。

例如,如果我们访问 website.com/clothes 或 website.com/clothes/tshirts 页面加载正常并且图像正确显示且没有错误。如果我们导航到 website.com/clothes/tshirts/bluepinned 产品图片 (website.com/images/clothes/tshirts/bluepinned.jpg) 无法加载,在 firebug 中出现 406 错误。如果我们缓存刷新页面(CTRL + F5),所有页面资源(如样式表、图像等)都会产生 406 错误,如果您再次刷新,整个服务器似乎超时并且什么都没有显示。如果我重新启动路由器(给我一个新的 IP 地址),我可以再次访问该站点。

网址:

website.com/clothes <- category showing all sub-categories and products (using product thumb image)
website.com/clothes/tshirts <- sub-category showing all products (using product thumb image)
website.com/clothes/tshirts/bluepinned <- individual product page (using large product image)

.htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(clothes)/([^/\.]+)/([^/\.]+)/?$ products.php?c=$1&sc=$2&p=$3 [NC,L,QSA] 
RewriteRule ^(clothes)/([^/\.]+)/?$ products.php?c=$1&sc=$2 [NC,L,QSA] 
RewriteRule ^(clothes)/?$ products.php?c=$1 [NC,L,QSA] 

我检查了 phpinfo - 没有找到 mod_security。在那些正在加载的页面上没有什么特别的 - 基本的 HTML 页面,带有 PHP 类调用来加载类别/子类别/产品。没有 Javascript、POST 或 AJAX。

任何想法可能导致 406?

更新 1: 这是 Firebug/Firefox 中的标头响应(已删除 Host 和 Referrer):

Response Headers:
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Type    text/html
Date    Thu, 23 Aug 2012 05:36:54 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=5, max=99
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
X-Powered-By    PHP/5.2.17

Request Headers:
Accept  image/png,image/*;q=0.8,*/*;q=0.5
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  __utma=175298877.1838807483.1343995016.1345683454.1345698456.35; __utmz=175298877.1344487166.16.3.utmcsr=google.com|utmccn=(referral)|utmcmd=referral|utmcct=/imgres; __utmc=175298877; PHPSESSID=0d6664f0306618ac0f4218895efc4404; __utmb=175298877.2.10.1345698456
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1

更新 2: Google Chrome 中的标头响应:

Request Method:GET
Status Code:406 Not Acceptable

Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:PHPSESSID=d5b28960a6d3e73c7db5a3808432ee71; __utma=175298877.1644911186.1345701704.1345701704.1345701704.1; __utmb=175298877.2.10.1345701704; __utmc=175298877; __utmz=175298877.1345701704.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1

Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Type:text/html
Date:Thu, 23 Aug 2012 06:02:16 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=99
Pragma:no-cache
Server:Apache
Transfer-Encoding:chunked
X-Powered-By:PHP/5.2.17

更新 3: 添加了本地响应(有效!)

Response Headers
Accept-Ranges   bytes
Connection  Keep-Alive
Content-Length  28212
Content-Type    image/jpeg
Date    Thu, 23 Aug 2012 05:58:31 GMT
Etag    "40000000cd15a-6e34-4c6eb29ada7fc"
Keep-Alive  timeout=5, max=77
Last-Modified   Fri, 10 Aug 2012 15:40:25 GMT
Server  Apache/2.2.21 (Win64) mod_ssl/2.2.21 OpenSSL/1.0.0d PHP/5.3.8

Request Headers
Accept  image/png,image/*;q=0.8,*/*;q=0.5
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  PHPSESSID=mjete8dhhn9abi9fkdfkce0kh2
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1

所以我想如果文件是 .jpg,image/* 会允许吗?

【问题讨论】:

  • 传回的数据类型与“请求者”接受的不匹配? checkupdown.com/status/E406.html 查看客户端期望的数据类型以及您的 Web 服务器返回的数据类型。
  • @nemoden 这怎么能解释为什么如果我重新启动路由器,网站会再次运行? (目前该网站不适合我)
  • 特定路由器设置或病毒在重新启动时不加载,但在一段时间后?我不知道,我只是指出这个问题来自哪里,并建议你检查是否是这样。
  • @Nemoden 我刚刚添加了标题响应。有什么想法吗?正在加载的产品图片出现 406 错误是一个 .jpg 文件
  • 现在很清楚:您的浏览器正在访问图像,但您得到的是text/html。你能尝试用其他浏览器做你所做的事情吗(谷歌浏览器是一个不错的选择)?

标签: apache .htaccess redirect url-rewriting


【解决方案1】:

您必须使用REQUEST_URI 而不是REQUEST_FILENAME 或将本地路径附加到您的重写规则中

来自 apache 文档:

REQUEST_URI

请求的 URI 的路径组件,例如 “/index.html”。这特别排除了查询字符串,它是 可作为其自己的名为 QUERY_STRING 的变量使用。

REQUEST_FILENAME

匹配的文件或脚本的完整本地文件系统路径 请求,如果这已经由服务器当时确定 引用了 REQUEST_FILENAME。否则,例如在使用时 虚拟主机上下文,与 REQUEST_URI 的值相同。

还可以查看 LogLevel alert rewrite:trace3 以清楚了解替换的内容和匹配的内容

【讨论】:

  • 踪迹说了什么?你能复制它的输出吗?注意 REQUEST_FILENAME 在虚拟主机上的行为不同
猜你喜欢
  • 2012-07-23
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 2015-10-15
  • 2012-11-22
相关资源
最近更新 更多