Article directory
Copyright statement: This article is an original article by CSDN blogger "Yang Qun". It follows the CC 4.0 BY-SA copyright agreement and was first published on CSDN on August 27, 2023. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/u011046671/article/details/132526618
1. Basic environment
Operating system: Microsoft Windows 10 Professional Edition
nginx version: 1.25.1
2. Applicable scenarios
Use nginx to build a simple file download server.
3. Methods and steps
The configuration file is as follows:
# 工作进程:根据需要进行设置,一般设置为CPU核心数的2倍或直接设置为auto由程序自动调整
worker_processes 1;
events {
# 每个工作进程的连接数,默认为1024个
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
charset utf-8;
root D:/Xufengsoft; # 文件存放目录
location / {
autoindex on; # 启用自动首页功能
autoindex_format html; # 首页格式为HTML
autoindex_exact_size on; # 文件大小自动换算
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 1024m; # 限制下载速度为1MB
limit_rate_after 1024m; # 当客户端下载速度达到2MB时进入限速模式
max_ranges 4096; # 客户端执行范围读取的最大值是4096B
send_timeout 20s; # 客户端引发传输超时时间为20s
postpone_output 2048; # 当缓冲区的数据达到2048B时再向客户端发送
chunked_transfer_encoding on; # 启用分块传输标识
}
}
}
4. Other instructions
The page is a bit ugly. There is a way to beautify it on the Linux platform, but there is no way to beautify it on the Windows platform.
Copyright statement: This article is an original article by CSDN blogger "Yang Qun". It follows the CC 4.0 BY-SA copyright agreement and was first published on CSDN on August 27, 2023. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/u011046671/article/details/132526618