【问题标题】:Apache sending incorrect response header for .js filesApache 为 .js 文件发送不正确的响应标头
【发布时间】:2012-12-31 01:31:33
【问题描述】:

我注意到 Chrome 报告 .js 文件的 MIME 类型错误。这些文件由 Apache 2 提供,最初在其 conf 文件中没有引用 js 文件。

我在http.conf中添加了如下配置:

AddType application/x-javascript .js

然后重新加载配置。 .js 文件仍在与此响应标头一起发送:

HTTP/1.1 200 OK
Date: Wed, 16 Jan 2013 16:39:42 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.4.10
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

没有 .htaccess 文件。

什么会导致 Apache 发送不正确的 Content-Type,即使我在配置中指定了 application/x-javascript 并重新加载了服务器?

【问题讨论】:

  • 这个标头似乎是由 PHP 解释器发送的(根据 X-Powered-By 属性)。

标签: javascript apache mime-types content-type


【解决方案1】:

我最近在另一台服务器上遇到了这个问题并解决了这个问题。 conf.d/php.conf 包含以下几行:

AddHandler php5-script .php .phtml .html .css .js
AddType text/html .php

我把第一行改成了

AddHandler php5-script .php .phtml .html
AddType text/html .php

然后保存并重新启动 Apache。 js & css 文件现在以正确的 mime 类型出现在 Firefox/Chrome 中。

顺便说一句,我尝试在 AddType text/html .php 下方添加 AddType text/javascript .js,但文件在 Inspector 中仍显示为 text/html。我必须从 AddHandler 中删除 .css 和 .js 才能发送正确的 mime 类型。

【讨论】:

    【解决方案2】:

    如果您的 httpd.conf 中有 SetHandler application/x-httpd-php 之类的内容,则应将其放入 <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> 以让 Apache 为其他文件发送良好的 Content-Type。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2020-05-05
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2014-09-06
      相关资源
      最近更新 更多