apache下载docx,pptx文件被浏览器当作zip文件保存:修改apache MIME类型配置mime.types

2018-01-10服务器
295

apache web服务器的站点,下载pptx,docx,xlsx文件,却被浏览器当作zip文件保存。
 
这不是浏览器的问题,而是apache不认docx,pptx,,xlsx等Microsoft Office 2007+的文件格式,而这些文件本身是zip压缩文件,所以被apache当作zip压缩文件发给浏览器了。
 
做个形像一点的比喻:浏览器请求一个url路径,这个路径指向一个.pptx文件,apache从web服务器的目录(磁盘上)查到这个文件,但它不知道这个文件是什么类型(注意:文件类型并不是由文件名后缀决定的,而是文件本身内容所决定),于是它通过分析这个文件内容,发现这个文件像是个zip压缩档,文件大小为12345字节,于是它给浏览器说:“嘿,你请求的是个zip压缩文件,它的大小为12345个字节,文件名是 abcd.pptx 。下面是这个文件的内容,请接收”
 
浏览器收到消息后,得知apache要发给自己一个zip压缩档,于是一边弹出对话框、询问用户是直接打开、还是保存到硬盘,一边把apache接下来发回的数据保存到缓冲区(或者保存到硬盘的临时目录里),等用户做好选择后,采取相应的打开或是保存的操作。
 
形象的说明,并不是十分准确的,但大体过程如此。
 
那现在的问题在于apache因为不认识这个pptx文件,那我们要教会apache,让它知道。我们可以通过mime.types文件让告诉它:
 
修改mime.types文件,在其中加上
 
application/vnd.ms-word.document.macroEnabled.12 docm
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
application/vnd.ms-powerpoint.template.macroEnabled.12 potm
application/vnd.openxmlformats-officedocument.presentationml.template potx
application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
application/vnd.ms-excel.addin.macroEnabled.12 xlam
application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.ms-excel.template.macroEnabled.12 xltm
application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx
这样的一部分代码,这里包含了ms office相关的很多文件类型。
 
mime.types文件位置
 
那mime.types文件在什么位置呢?
 
对于windows版本的apache,它在apache安装目录下的conf子目录里,典型位置为 X:\Program Files\Apache Group\Apache2\conf\mime.types
 
而linux版本下,通常位于/etc/mime.types, 如redhat/centos/fedora都是如此,debian/ubuntu系列应该也在这里,个人没有亲自查证。
 
不过centos并不推荐在这里修改,参看这里 http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-apache-config.html TypesConfig一小节
 
Instead of editing /etc/mime.types, the recommended way to add MIME type mappings is to use the AddType directive.
 
建议不要去修改 /etc/mime.types ,添加MIME type的推荐方法是使用apache的 AddType 指令.
 
AddType application/vnd.ms-word.document.macroEnabled.12 docm
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
AddType application/vnd.ms-powerpoint.template.macroEnabled.12 potm
AddType application/vnd.openxmlformats-officedocument.presentationml.template potx
AddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
AddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
AddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
AddType application/vnd.ms-excel.addin.macroEnabled.12 xlam
AddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
AddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
AddType application/vnd.ms-excel.template.macroEnabled.12 xltm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx
可以把这些指令加到apache的配置文件里,该指令在apache里出现过N次,不需多说,照着加进去就对了。
 
The End

相关推荐

Referer等错误码处理-爬虫反扒机制处理
HTTP来源地址(referer,或 HTTP referer)是HTTP表头的一个字段,用来表示从哪儿链接到目前的网页,采用的格式是URL。换句话说,借着HTTP来源地址,目前的网页可以检查访客从哪里而来,这也常被用来对付伪造的跨网站请求。网站里面常常会用到这个属性来对爬...
2022-12-12 服务器
322

Nginx使用中文文件名中文目录404错误的解决办法
一:确定你的系统是UTF编码 [root@Tserver ~]# env|grep LANG LANG=en_US.UTF-8 二:NGINX配置文件里默认编码设置为utf-8 server { listen 80; server_name .inginx.com ; index index.html index.htm index.php; root /usr/local/nginx/html/inginx.com; ch...
2022-10-14 服务器
902

网站无法加载woff字体文件的解决办法?
有客户反馈在安装网站后,woff、woff2字体无法加载,导致无法显示图标文件,这种情况要怎么解决呢? 这是因为服务器IIS默认是没有SVG,WOFF,WOFF2这几个文件类型的扩展的,所以如果要解决,我们就要在IIS上添加MIME 类型即可。 解决方法: 1、打开服务器IIS管...
2021-05-10 服务器
1249

网站服务器不支持.7z文件下载的解决方法
7-Zip是一款号称有着现今最高压缩比的压缩软件,它不仅支持独有的7z文件格式,而且还支持各种其它压缩文件格式,其中包括ZIP, RAR, CAB, GZIP, BZIP2和TAR。此软件压缩的压缩比要比普通ZIP文件高30-50%。因此,它可以把经WinZip压缩的文件再压缩2-10%。文件...
2020-09-28 服务器
428

安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
安装 Apache 出现 OS 10013 以一种访问权限不允许的方式做了一个访问套接字的尝试 如下截图: 提示: make_sock: could not bind to address 0.0.0.0:80 这个问题有由于计算机上安装了IIS7,80端口已占用。 打开Apache 的配置文件 Apache安装目录下的conf/htt...
2019-11-06 服务器
234

nginx加载伪静态文件.htaccess的办法
很多网站需要采用伪静态来访问动态网页。所以像phpcms这样的系统,都提供了一个.htaccess文件,供写伪静态规则。但这个规则是针对Apache的。在nginx服务器上并不起作用。 nginx 可以直接将伪静态规则写在配置文件中,但这个需要有服务器管理权限,普通站长不...
2019-01-16 服务器
544