当前位置: 代码网 > 服务器>服务器>Nginx > Nginx中的文件下载服务器详解

Nginx中的文件下载服务器详解

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

1.概述

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

    server {
        listen 8080;
        server_name  localhost;
        charset utf-8;
        root    /opt/nginx-web/files;             # 文件存放目录
        # 下载
        location / {
            autoindex on;                         # 启用自动首页功能
            autoindex_format html;                # 首页格式为html
            autoindex_exact_size off;             # 文件大小自动换算
            autoindex_localtime on;               # 按照服务器时间显示文件时间
            default_type application/octet-stream;# 将当前目录中所有文件的默认mime类型设置为
                                              # application/octet-stream
            if ($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调用的最大传输量为1mb
            tcp_nopush on;                        # 启用最小传输限制功能
            aio on;                               # 启用异步传输
            directio 5m;                          # 当文件大于5mb时以直接读取磁盘的方式读取文件
            directio_alignment 4096;              # 与磁盘的文件系统对齐
            output_buffers 4 32k;                 # 文件输出的缓冲区大小为128kb
            limit_rate 1m;                        # 限制下载速度为1mb
            limit_rate_after 2m;                  # 当客户端下载速度达到2mb时进入限速模式
            max_ranges 4096;                      # 客户端执行范围读取的最大值是4096b
            send_timeout 20s;                     # 客户端引发传输超时时间为20s
            postpone_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;                # 首页格式为html
            autoindex_exact_size off;             # 文件大小自动换算
            autoindex_localtime on;               # 按照服务器时间显示文件时间
            default_type application/octet-stream;# 将当前目录中所有文件的默认mime类型设置为
                                              # application/octet-stream
            if ($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调用的最大传输量为1mb
            tcp_nopush on;                        # 启用最小传输限制功能
            aio on;                               # 启用异步传输
            directio 5m;                          # 当文件大于5mb时以直接读取磁盘的方式读取文件
            directio_alignment 4096;              # 与磁盘的文件系统对齐
            output_buffers 4 32k;                 # 文件输出的缓冲区大小为128kb
            limit_rate 1m;                        # 限制下载速度为1mb
            limit_rate_after 2m;                  # 当客户端下载速度达到2mb时进入限速模式
            max_ranges 4096;                      # 客户端执行范围读取的最大值是4096b
            send_timeout 20s;                     # 客户端引发传输超时时间为20s
            postpone_output 2048;                 # 当缓冲区的数据达到2048b时再向客户端发送
            chunked_transfer_encoding on;         # 启用分块传输标识
        }
    }
}

2.2 共享文件目录

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

2.3 测试

        直接访问http://11.0.1.18:8080

到此这篇关于nginx之文件下载服务器的文章就介绍到这了,更多相关nginx文件下载服务器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com