5.20. Nginx文件下载服务器搭建¶
5.20.1. 1. Nginx作为文件服务器¶
nginx.conf文件配置如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include conf.d/*.conf;
}
conf.d/download_server.conf
[root@localhost nginx]# cat conf.d/download_server.conf
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; # 启用分块传输标识
}
}
查看nginx启动的端口
[root@localhost nginx]# netstat -tunpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 14108/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14108/nginx: master
5.20.2. 2. Nginx 目录浏览基础与进阶¶
https://www.cnblogs.com/ssgeek/p/14224879.html#34-%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%BB%E9%A2%98