解决lighttpd运行后浏览器无法渲染html

栏目: 服务器 · 发布时间: 7年前

内容简介:好久以前遇到的问题,当时是在fl2440上移植谷歌了好久都没有答案,结果放弃继续折腾搞来搞去原来是http相应报文的锅,响应报文中必须以字段

好久以前遇到的问题,当时是在fl2440上移植 lighttpd ,成功启动 web 服务器后访问网页会出现下载提示框,原来浏览器并没有渲染 html 页面,而是把它当作文件下载来处理。

谷歌了好久都没有答案,结果放弃继续折腾 lighttpd ,转而移植 appweb+php ,直到上次在手机上成功用 termux 装上 wordpress 才重新解决了这个问题。

搞来搞去原来是http相应报文的锅,响应报文中必须以字段 Content-Type 指明媒体文件的类型,这样浏览器才能正确进行解析。

所以lighttpd的配置文件中必须有

mimetype.assign = ( 
".pdf"   => "application/pdf",
".class" => "application/octet-stream",
".pac"   => "application/x-ns-proxy-autoconfig",
".swf"   => "application/x-shockwave-flash",
".wav"   => "audio/x-wav",
".gif"   => "image/gif",
".jpg"   => "image/jpeg",
".jpeg"  => "image/jpeg",
".png"   => "image/png",
".svg"   => "image/svg+xml",
".css"   => "text/css",
".html"  => "text/html",
".htm"   => "text/html",
".txt"   => "text/plain",
".dtd"   => "text/xml",
".xml"   => "text/xml"
 )

接下来看看配置文件加和不加 之间有什么不同

含有 mimetype.assign 字段的响应报文头

-bash-4.4$ curl -I 127.0.0.1:10086
HTTP/1.1 200 OK
Content-Type: text/html
Accept-Ranges: bytes
ETag: "1739553501"
Last-Modified: Sun, 26 Feb 2017 01:21:41 GMT
Content-Length: 5
Date: Tue, 28 Feb 2017 12:55:27 GMT
Server: lighttpd/1.4.45

不含有 mimetype.assign 字段的响应报文头

-bash-4.4$ curl -I 127.0.0.1:10086
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Accept-Ranges: bytes
Content-Length: 5
Date: Tue, 28 Feb 2017 12:56:46 GMT
Server: lighttpd/1.4.45

果不其然, Content-Type 导致了浏览器解析效果的差异性,这说明了浏览器并不能通过后缀名来直接识别网页文件,必须通过相应报文来配合服务器进行页面渲染。


以上所述就是小编给大家介绍的《解决lighttpd运行后浏览器无法渲染html》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Speed Up Your Site

Speed Up Your Site

Andrew B. King / New Riders Press / 2003-01-14 / USD 39.99

There's a time bomb on the web: user patience. It starts ticking each time someone opens one of your pages. You only have a few seconds to get compelling content onto the screen. Fail, and you can kis......一起来看看 《Speed Up Your Site》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具