nginx content-disposition
时间: 2023-08-31 16:09:53 浏览: 49
在 Nginx 中,Content-Disposition 是一个 HTTP 响应头,用于指示浏览器如何处理响应的内容。它通常用于提供一个建议的文件名以供下载,或者指示浏览器直接在浏览器中打开响应的内容。
要在 Nginx 中设置 Content-Disposition 头,可以使用 add_header 指令。例如,以下配置将设置 Content-Disposition 为 "attachment",并指定建议的文件名为 "example.pdf":
```
location /download {
add_header Content-Disposition "attachment; filename=example.pdf";
//其他配置
}
```
这将提示浏览器下载 "example.pdf" 文件,而不是直接在浏览器中打开它。如果您想要直接在浏览器中打开响应的内容,可以将 Content-Disposition 设置为 "inline"。
相关问题
nginx下载文件配置
在nginx的配置文件中,可以通过添加一个路由来配置下载文件。具体步骤如下:
1. 打开nginx的配置文件:vi /usr/local/nginx/conf/nginx.conf
2. 在配置文件中添加一个路由:location /download {
3. 设置下载文件的根目录:root /data;
4. 添加Content-Disposition和Content-Type头信息,用于告诉浏览器下载文件的方式和类型:add_header Content-Disposition: "attachment"; add_header Content-Type application/octet-stream;
5. 开启目录浏览功能:autoindex off;
6. 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b:autoindex_exact_size off;
7. 开启以服务器本地时区显示文件修改日期:autoindex_localtime on;
8. 保存配置文件并重启nginx服务:/usr/local/nginx/sbin/nginx -s reload
注意:在配置文件中添加路由时,需要根据实际情况修改路由和根目录的路径。
用nginx怎么做预览
在使用 Nginx 进行预览时,可以使用 Nginx 的 sub_filter 模块和 add_header 模块实现。具体步骤如下:
1. 配置 Nginx 的 sub_filter 模块,将源代码中的链接地址替换为预览地址。例如,将 HTML 文件中的图片链接地址修改为预览地址。
2. 配置 Nginx 的 add_header 模块,添加 Content-Disposition 头信息及文件名。例如,将文件名设置为源文件名加上 "_preview" 后缀。
下面是一个示例配置文件:
```
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
location /preview/ {
sub_filter 'http://example.com/uploads/' 'http://preview.example.com/';
sub_filter_types text/html;
add_header Content-Disposition 'attachment; filename="$request_filename_preview"';
}
}
```
在上述示例中,我们将源代码中的图片链接地址 "http://example.com/uploads/" 替换为预览地址 "http://preview.example.com/",并在 Content-Disposition 头信息中添加文件名后缀 "_preview"。
使用上述配置后,访问 "http://example.com/preview/index.html" 就可以预览 HTML 文件,其中图片等资源的链接地址已经被替换为预览地址,并且浏览器中会提示下载文件,文件名为源文件名加上 "_preview" 后缀。