一、背景
我们在工作过程中,有许多大的镜像或者安装包等,占用了我们本地电脑大量空间,并且下载速度慢,搭建一个文件服务器,可以高效的储存文件,以及wget下载。
二、操作步骤
1、下载nginx安装包并安装(编译安装)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 下载nginx安装包,nginx官网地址:http: //nginx .org/ wget http: //nginx .org /download/nginx-1 .13.7. tar .gz // 安装nginx依赖包 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel // 创建nginx安装目录并解压安装包 mv nginx-1.13.7 nginx /usr/local/ tar -zxvf nginx-1.13.7. tar .gz // 进入nginx目录,安装证书,并安装nginx mv nginx-1.13.7 nginx cd nginx // 执行命令 考虑到后续安装ssl证书 添加两个模块 . /configure --with-http_stub_status_module --with-http_ssl_module //make 安装nginx make && make instal l // 启动nginx . /nginx |
2、修改nginx.cof配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // 进入conf目录 cd /usr/local/nginx/conf // 修改nginx.conf vim nginx.conf server { listen 80; server_name localhost; charset utf-8; root /home/downloads ; # 文件存放目录 # 下载 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; # 启用分块传输标识 } } |
3、重启nginx
1 2 3 4 | // 进入启动目录 cd /usr/local/nginx/sbin/ // 重启nginx . /nginx -s reload |
三、效果
1、效果图(浏览器访问http://+ip)
2、模拟其他服务器来文件服务器wget下载文件(喔的天,93M/S)
到此这篇关于nginx搭建文件服务器(保姆级)的文章就介绍到这了,更多相关nginx搭建文件服务器内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!