博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
032_nginx配置文件安全下载
阅读量:6629 次
发布时间:2019-06-25

本文共 3072 字,大约阅读时间需要 10 分钟。


一、

server {    listen 8866;    server_name _;    access_log /usr/local/etc/nginx/log/download.access.log main;    error_log /usr/local/etc/nginx/log/download.error.log;    location / {      root /usr/local/etc/nginx/aruntestdir;      if ($request_filename ~* ^.*?\.(html|zip|gz)$){     #不能为空              add_header Content-Disposition: 'attachment;';	      add_header Content-Type: 'APPLICATION/OCTET-STREAM';      }    }}

测试:

如下图所示在nginx的返回的网页Respons中添加头字段.

 

Request头解释:

<1>$request_filename

file path for the current request, based on the root or alias directives, and the request URI

<2>Content-Disposition

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.    attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog

Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

<3>Content-Type

The Content-Type entity header is used to indicate the media type of the resource.In responses, a Content-Type header tells the client what the content type of the returned content actually is.    application/octet-stream meaning "download this file"扩展:What are MIME types?MIME types describe the media type of content either in email or served by web servers or web applications and are intended to help guide a web browser in how the content is to be processed and displayed. Examples of MIME types are:text/html for normal web pagestext/plain for plain texttext/css for Cascading Style Sheetstext/javascript for scriptsapplication/octet-stream meaning "download this file"application/x-java-applet for Java appletsapplication/pdf for PDF documents

Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type 

二、附下载python脚本

#!/usr/bin/env pythonimport osimport urllibdef Schedule(blocks, blocksize, totalsizeofile):    """    The third argument, if present, is a hook function that will be called once on establishment of the network connection    and once after each block read thereafter.    The third argument may be -1 on older FTP servers which do not return a file size in response to a retrieval request.    The hook will be passed three arguments;    :param blocks: a count of blocks transferred so far    :param blocksize:  a block size in bytes    :param totalsizeofile: the total size of the file.    :return:    """    percent = 100.0 * blocks * blocksize / totalsizeofile    if percent > 100 :        percent = 100    print '%.2f %%' % percentif __name__ == "__main__":    url = 'http://127.0.0.1:8088/downtest.html'    #download url    local = os.path.join('/tmp','downtest.html')   #local path + filename    #If no Content-Length header was supplied, urlretrieve() can not check the size of the data it has downloaded,    # and just returns it. In this case you just have to assume that the download was successful.    urllib.urlretrieve(url, local, Schedule)

 

转载地址:http://ftwvo.baihongyu.com/

你可能感兴趣的文章
linux NFS
查看>>
Jquery DataTable基本使用
查看>>
leetcode 674. Longest Continuous Increasing Subsequence
查看>>
Extensions in UWP Community Toolkit - SurfaceDialTextbox
查看>>
Java中CAS详解
查看>>
Linux系统实战项目——sudo日志审计
查看>>
Android Application Task Activities的关系
查看>>
浅谈CSS盒子模型
查看>>
实现iFrame自适应高度,原来很简单!
查看>>
get app id
查看>>
poj 3624 0/1背包暨0/1背包的学习
查看>>
[俗一下]世界500强公司的面试问题与答案提示 [转]
查看>>
使用 Excel Services ,结合 Analysis Services 在 SharePoint 中发布报表
查看>>
SQL Server数据导入导出技术概述与比较
查看>>
format的用法
查看>>
DHCPv6 server port and DHCPv6 client port
查看>>
BitmapFactory.Options避免 内存溢出 OutOfMemoryError的优化方法
查看>>
Python中通过Image的open之后,去show结果打不开bmp图片,无法正常显示图片
查看>>
DNGuard 免费的DotNet加密保护工具 V1.0
查看>>
编程中的命名设计
查看>>