Nginx中的文件下載服務(wù)器詳解
1.概述
在對(duì)外分享文件時(shí),利用Nginx搭建一個(gè)簡(jiǎn)單的下 載文件管理服務(wù)器,文件分享就會(huì)變得非常方便。利 用Nginx的諸多內(nèi)置指令可實(shí)現(xiàn)自動(dòng)生成下載文件列表 頁(yè)、限制下載帶寬等功能。配置樣例如下:
server { listen 8080; server_name localhost; charset utf-8; root /opt/nginx-web/files; # 文件存放目錄 # 下載 location / { autoindex on; # 啟用自動(dòng)首頁(yè)功能 autoindex_format html; # 首頁(yè)格式為HTML autoindex_exact_size off; # 文件大小自動(dòng)換算 autoindex_localtime on; # 按照服務(wù)器時(shí)間顯示文件時(shí)間 default_type application/octet-stream;# 將當(dāng)前目錄中所有文件的默認(rèn)MIME類型設(shè)置為 # application/octet-stream if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){ # 當(dāng)文件格式為上述格式時(shí),將頭字段屬性Content-Disposition的值設(shè)置為"attachment" add_header Content-Disposition: 'attachment;'; } sendfile on; # 開(kāi)啟零復(fù)制文件傳輸功能 sendfile_max_chunk 1m; # 每個(gè)sendfile調(diào)用的最大傳輸量為1MB tcp_nopush on; # 啟用最小傳輸限制功能 aio on; # 啟用異步傳輸 directio 5m; # 當(dāng)文件大于5MB時(shí)以直接讀取磁盤(pán)的方式讀取文件 directio_alignment 4096; # 與磁盤(pán)的文件系統(tǒng)對(duì)齊 output_buffers 4 32k; # 文件輸出的緩沖區(qū)大小為128KB limit_rate 1m; # 限制下載速度為1MB limit_rate_after 2m; # 當(dāng)客戶端下載速度達(dá)到2MB時(shí)進(jìn)入限速模式 max_ranges 4096; # 客戶端執(zhí)行范圍讀取的最大值是4096B send_timeout 20s; # 客戶端引發(fā)傳輸超時(shí)時(shí)間為20s postpone_output 2048; # 當(dāng)緩沖區(qū)的數(shù)據(jù)達(dá)到2048B時(shí)再向客戶端發(fā)送 chunked_transfer_encoding on; # 啟用分塊傳輸標(biāo)識(shí) } }
2.實(shí)驗(yàn)
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; # 啟用自動(dòng)首頁(yè)功能 autoindex_format html; # 首頁(yè)格式為HTML autoindex_exact_size off; # 文件大小自動(dòng)換算 autoindex_localtime on; # 按照服務(wù)器時(shí)間顯示文件時(shí)間 default_type application/octet-stream;# 將當(dāng)前目錄中所有文件的默認(rèn)MIME類型設(shè)置為 # application/octet-stream if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){ # 當(dāng)文件格式為上述格式時(shí),將頭字段屬性Content-Disposition的值設(shè)置為"attachment" add_header Content-Disposition: 'attachment;'; } sendfile on; # 開(kāi)啟零復(fù)制文件傳輸功能 sendfile_max_chunk 1m; # 每個(gè)sendfile調(diào)用的最大傳輸量為1MB tcp_nopush on; # 啟用最小傳輸限制功能 aio on; # 啟用異步傳輸 directio 5m; # 當(dāng)文件大于5MB時(shí)以直接讀取磁盤(pán)的方式讀取文件 directio_alignment 4096; # 與磁盤(pán)的文件系統(tǒng)對(duì)齊 output_buffers 4 32k; # 文件輸出的緩沖區(qū)大小為128KB limit_rate 1m; # 限制下載速度為1MB limit_rate_after 2m; # 當(dāng)客戶端下載速度達(dá)到2MB時(shí)進(jìn)入限速模式 max_ranges 4096; # 客戶端執(zhí)行范圍讀取的最大值是4096B send_timeout 20s; # 客戶端引發(fā)傳輸超時(shí)時(shí)間為20s postpone_output 2048; # 當(dāng)緩沖區(qū)的數(shù)據(jù)達(dá)到2048B時(shí)再向客戶端發(fā)送 chunked_transfer_encoding on; # 啟用分塊傳輸標(biāo)識(shí) } } }
2.2 共享文件目錄
[root@ansible01 files]# tree /opt/nginx-web/files/ /opt/nginx-web/files/ ├── doc └── txt 2 directories, 0 files
2.3 測(cè)試
直接訪問(wèn)http://11.0.1.18:8080
到此這篇關(guān)于Nginx之文件下載服務(wù)器的文章就介紹到這了,更多相關(guān)Nginx文件下載服務(wù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx?http?499錯(cuò)誤碼詳解以及解決辦法
HTTP狀態(tài)碼出現(xiàn)499錯(cuò)誤有多種情況,499錯(cuò)誤是什么?這篇文章主要給大家介紹了關(guān)于nginx?http?499錯(cuò)誤碼以及解決辦法的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01nginx上部署react項(xiàng)目的實(shí)例方法
今天小編就為大家分享一篇關(guān)于nginx上部署react項(xiàng)目的實(shí)例方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Nginx反向代理+DNS輪詢+IIS7.5 千萬(wàn)PV 百萬(wàn)IP 雙線 網(wǎng)站架構(gòu)案例
某公司有一站點(diǎn),一天IP 430W,PV 3100W,之前采用5臺(tái) DELL R610 做NLB,系統(tǒng)2008 IIS7.5.每天高峰期時(shí)都不堪重負(fù).會(huì)出現(xiàn)以下情況2012-11-11nginx將https協(xié)議反向代理到http協(xié)議請(qǐng)求上
在項(xiàng)目正式上線時(shí),一般會(huì)申請(qǐng)域名和證書(shū)來(lái)實(shí)現(xiàn)https的服務(wù),本文主要介紹了nginx將https協(xié)議反向代理到http協(xié)議請(qǐng)求上,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Dockerfile打包nginx鏡像的實(shí)現(xiàn)步驟
本文主要介紹了Dockerfile打包nginx鏡像的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10