nginx 在线预览与强制下载

环境如下:

nginx version: nginx/1.14.1
nginx version: nginx/1.16.1
Chrome:102.0.5005.63(正式版本) (64 位)
CentOS Linux release 7.5.1804 (Core) 

将任意类型文件设置成 在线预览 或者 直接下载

.log.txt 文件为例,nginx 默认配置下 .txt是可以在线打开,而.log 会有弹窗,也就是下载。

使用是nginx,配置实现如下:

#################default_type 方式 ##################
# 配置在线预览
location ~ .*\.log$ {
    default_type text/plain; 
    charset utf-8;
}
# 配置下载文件模式
location ~ .*\.txt$ {
    types        { }
    default_type application/octet-stream; 
}
#################add_header  方式 ##################
# 配置在线预览
location ~ .*\.log$ {
    add_header Content-Type "text/plain; 
    charset=utf-8";
}
# 配置下载文件模式
location ~ .*\.txt$ {
    types        { }
    add_header Content-Type "application/octet-stream; 
    charset=utf-8";
}
#################修改 mime.types方式##################
vi mime.types #新增如下
types {
    ···
    # 配置全局txt文件下载
    application/octet-stream        txt;

    # 配置全局log文件在线预览
    text/plain                      log;
}
# 影响nginx 全局配置,个人不太推荐。

##############Content-Disposition 方式###############
# 配置附件下载模式
location ~ .*\.txt$ {
    add_header Content-Disposition "attachment";
}

为什么浏览器访问不同的类型的文件会出现不同结果呢

简单的来说浏览器一般会根据nginx的Response Headers中返回的content-type 来识别处理渲染文件,例如常见的格式 有text/html HTML文档格式、text/plain 纯文本格式。打开浏览器开发工具可以看到:

Response Headers:
  content-encoding: gzip
  content-language: zh-CN
  content-type: text/html;charset=utf-8

或者用命令的方式,可以看到 Content-Type 字段:

[root@tserver121 conf]# curl http://localhost/packer.log -I
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
······

NGINX是如何配置生效的

我们可以看nginx.conf配置文件中有一段配置:

http {
    include       mime.types;
    default_type  application/octet-stream;
    ···
}

application/octet-stream: 大多数浏览器会将其视为二进制文件并下载

default_type 仅适用于未在mime.types文件中定义的文件扩展名

首先nginx会读取mime.types 中定义好的 数据类型与文件类型关系。然后使用default_typemime.types 中未定义的的都设置为application/octet-stream:

官方是如何定义下载目录的

A sufficiently full mapping table is distributed with nginx in the conf/mime.types file.
To make a particular location emit the “application/octet-stream” MIME type for all requests, the following configuration can be used:

location /download/ {
    types        { }
    default_type application/octet-stream;
}

Syntax:	default_type mime-type;
Default:	
default_type text/plain;
Context:	http, server, location
Defines the default MIME type of a response. Mapping of file name extensions to MIME types can be set with the types directive.

location匹配规则不对导致配置不生效

nginx 在postread 阶段对它的配置文件解析,对location解析,是先进行前缀匹配,再进行一般匹配,一般匹配是按照最长匹配规则。

前缀匹配包含:

=	#精确匹配              匹配后不在进行匹配
^~	#                     匹配后不再进行正则匹配
~	#正则匹配   			  正则匹配是有顺序的
~*	#忽略大小写,正则匹配     正则匹配是有顺序的

如果是^~开头的匹配,则不会继续搜索正则匹配,但是会继续搜索一般匹配
普通匹配:无顺序,是按匹配长短来确定的   
正则匹配:有顺序,是从前往后匹配的

少数浏览器导致配置不生效

各个浏览器对常用或者错误的 Content-Type 类型处理方式也有不一致的地方,比如edge,就能配置是否在浏览器中打开 Office 文件,有的浏览器因为特殊字符而导致文件下载。

Content-disposition 是什么

Content-disposition 是 MIME 协议的扩展,ontent-type 指示响应内容的格式;content-disposition` 指示如何处理响应内容。关于Content-Disposition: inline 可以 点击查看

参阅:

nginx常用配置方法

反向 ssl 代理 成功案例

各浏览器对常用或者错误的 Content-Type 类型处理方式不一致

服务器响应包含非法字符可能导致下载

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值