当前位置: 首页 > news >正文

Nginx之文件下载服务器

1.概述

        在对外分享文件时,利用Nginx搭建一个简单的下 载文件管理服务器,文件分享就会变得非常方便。利 用Nginx的诸多内置指令可实现自动生成下载文件列表 页、限制下载带宽等功能。配置样例如下:

    server {listen 8080;server_name  localhost;charset utf-8;root    /opt/nginx-web/files;             # 文件存放目录# 下载location / {autoindex on;                         # 启用自动首页功能autoindex_format html;                # 首页格式为HTMLautoindex_exact_size off;             # 文件大小自动换算autoindex_localtime on;               # 按照服务器时间显示文件时间default_type application/octet-stream;# 将当前目录中所有文件的默认MIME类型设置为# application/octet-streamif ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){# 当文件格式为上述格式时,将头字段属性Content-Disposition的值设置为"attachment"add_header Content-Disposition: 'attachment;';}sendfile on;                          # 开启零复制文件传输功能sendfile_max_chunk 1m;                # 每个sendfile调用的最大传输量为1MBtcp_nopush on;                        # 启用最小传输限制功能aio on;                               # 启用异步传输directio 5m;                          # 当文件大于5MB时以直接读取磁盘的方式读取文件directio_alignment 4096;              # 与磁盘的文件系统对齐output_buffers 4 32k;                 # 文件输出的缓冲区大小为128KBlimit_rate 1m;                        # 限制下载速度为1MBlimit_rate_after 2m;                  # 当客户端下载速度达到2MB时进入限速模式max_ranges 4096;                      # 客户端执行范围读取的最大值是4096Bsend_timeout 20s;                     # 客户端引发传输超时时间为20spostpone_output 2048;                 # 当缓冲区的数据达到2048B时再向客户端发送chunked_transfer_encoding on;         # 启用分块传输标识}}

2.实验

        2.1 nginx配置文件

[root@ansible01 nginx]# cat /etc/nginx/nginx.conf|grep -v "^$"|grep -v "^#"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {worker_connections 1024;
}
http {log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile            on;tcp_nopush          on;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 4096;include             /etc/nginx/mime.types;default_type        application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;# Load configuration files for the default server block.server {listen 8080;server_name  11.0.1.18;charset utf-8;root    /opt/nginx-web/files;             # 文件存放目录# 下载location / {autoindex on;                         # 启用自动首页功能autoindex_format html;                # 首页格式为HTMLautoindex_exact_size off;             # 文件大小自动换算autoindex_localtime on;               # 按照服务器时间显示文件时间default_type application/octet-stream;# 将当前目录中所有文件的默认MIME类型设置为# application/octet-streamif ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){# 当文件格式为上述格式时,将头字段属性Content-Disposition的值设置为"attachment"add_header Content-Disposition: 'attachment;'; }sendfile on;                          # 开启零复制文件传输功能sendfile_max_chunk 1m;                # 每个sendfile调用的最大传输量为1MBtcp_nopush on;                        # 启用最小传输限制功能aio on;                               # 启用异步传输directio 5m;                          # 当文件大于5MB时以直接读取磁盘的方式读取文件directio_alignment 4096;              # 与磁盘的文件系统对齐output_buffers 4 32k;                 # 文件输出的缓冲区大小为128KBlimit_rate 1m;                        # 限制下载速度为1MBlimit_rate_after 2m;                  # 当客户端下载速度达到2MB时进入限速模式max_ranges 4096;                      # 客户端执行范围读取的最大值是4096Bsend_timeout 20s;                     # 客户端引发传输超时时间为20spostpone_output 2048;                 # 当缓冲区的数据达到2048B时再向客户端发送chunked_transfer_encoding on;         # 启用分块传输标识}}
}

        2.2 共享文件目录

[root@ansible01 files]# tree /opt/nginx-web/files/
/opt/nginx-web/files/
├── doc
└── txt2 directories, 0 files

        2.3 测试

        直接访问http://11.0.1.18:8080

http://www.lryc.cn/news/370987.html

相关文章:

  • OpenCV学习(4.11) OpenCV中的图像转换
  • 2024.6.13每日一题
  • Linux命令详解(2)
  • iOS ReactiveCocoa MVVM
  • 图文解析ASN.1中BER编码:结构类型、编码方法、编码实例
  • jQuery如何停止动画队列
  • vue3+electron搭建桌面软件
  • oracle常用经典SQL查询
  • Android shell 常用 debug 命令
  • Unity3D Shader数据传递语法详解
  • 计算机组成原理(五)
  • 后端项目实战--瑞吉外卖项目软件说明书
  • LeetCode | 27.移除元素
  • 为什么要选择AWS?AWS的优势有哪些?
  • 【Intel CVPR 2024】通过图像扩散模型生成高质量360度场景,只需要一个语言模型
  • postman教程-21-Newman运行集合生成测试报告
  • 基于条件谱矩的时间序列分析(以轴承故障诊断为例,MATLAB)
  • ArcGIS Pro 3.0加载在线高德地图
  • 服务器防漏扫,主机加固方案来解决
  • Linux2(基本命令2)
  • 拼团+秒杀+优惠折扣+个人免签双端商城源码
  • 【数据结构】双向链表(C语言)
  • 【TensorFlow深度学习】WGAN与DCGAN在图像生成中的应用实例
  • 垫付商贩任务补单平台补单系统网站源码提供
  • vue富文本wangeditor加@人功能(vue2 vue3都可以)
  • ######## redis各章节终篇索引(更新中) ############
  • 一个基于MySQL的数据库课程设计的基本框架
  • 架构设计基本原则
  • 云原生应用开发培训,开启云计算时代的新征程
  • 【数据库设计】宠物商店管理系统