nginx使用XSendfile模块让php更快下载文件实战
https://www.markdream.com/technologies/programs/nginx-x-accel-redirect-php-practise.shtml?utm_source=tuicool&utm_medium=referral
引言
最近有点闲暇时间了,[email protected]
让PHP更快的提供文件下载》后,利用实际项目中的业务场景觉得有必须要去优化附件下载功能了。鸟哥的文章里面主要介绍的基于apache来做XSendfile讲解的,我有点强迫症,我对我的生产环境《顶岗实习管理系统》进行升级改造,我的webserver是nginx,所以基于nginx官方网站的 XSendfile说明特做此改造笔记。
我的webroot目录在/var/vhost/demo,demo目录中存在一个uploadfiles文件夹,这个文件夹是存放用户上传的文件。比如一个用户上传一个hello.docx文件,那么它对应的物理路径应该是/var/vhost/demo/xsendfile/uploadfiles/hello.docx,如果我们以直接路径“http://demo.markdream.com/xsendfile/uploadfiles/hello.docx”来访问的话,那么我想对这个文件进行下载统计或者隐藏实体路径的话就只能使用php readfile()方法来把文件装载到内存中,在转发给客户端,如果文件超大的话,你的服务器估计很悬。所以我们要改进php下载方式,就是引言所阐述的利用nginx的XSendfile方式来传送文件给客户端,nginx默认已经包含了senfile模块了,你如果安装完nginx你会在nginx.conf文件会看到“sendfile on;”,OK说明你可以直接使用了不像apache那么复杂还要重新加载组件编译等等。好了我贴下我的配置文件和代码。
我的nginx虚拟主机配置文件
server { listen 80; server_name demo.markdream.com; root /var/vhost/demo; index index.php; # 这个是定义读取你的文件的目录的url开头 直接访问是不可以的 只能通过 location /protected { internal; alias /var/vhost/demo/uploadfiles; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }}
一个简单的php下载文件脚本,downloads.php
<?php//eg: http://demo.markdream.com/xsendfile/downloads.php?filename=hello.docx// 获取文件名$filename = $_GET["filename"];// 你可以在这里写下你的查询数据库等你所想的功能 ……header ( "Content-type: application/octet-stream" );// 处理中文文件名$ua = $_SERVER ["HTTP_USER_AGENT"];if (preg_match ( "/MSIE/", $ua )) { $encoded_filename = rawurlencode ( $filename ); header ( 'Content-Disposition: attachment; filename="' . $encoded_filename . '"' );} else if (preg_match ( "/Firefox/", $ua )) { header ( "Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"' );} else { header ( 'Content-Disposition: attachment; filename="' . $filename . '"' );}// 就这么简单一句话搞定 注意“protected”是和nginx配置文件的 protected要一致header("X-Accel-Redirect: /protected/" . $filename);?>
愉悦的demo
http://demo.markdream.com/xsendfile/downloads.php?filename=hello.docx
参考
- http://wiki.nginx.org/XSendfile
- http://www.laruence.com/2012/05/02/2613.html
- http://kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器