Nginx uses the XSendfile module to allow php to download files faster

Source: Internet
Author: User
Nginx uses the XSendfile module to allow php to download files faster. In practice, nginx uses the XSendfile module to allow php to download files faster.

Https://www.markdream.com/technologies/programs/nginx-x-accel-redirect-php-practise.shtml? Utm_source = tuicool & utm_medium = referral

Introduction

It's a bit of time recently. [email protected]

After PHP enables faster file downloads, it is necessary to optimize the attachment download function in actual project business scenarios. Laruence's article mainly introduces XSendfile based on apache. I am a little obsessive. I have upgraded and transformed my production environment "Post practice management system, my webserver is nginx, so it is based on the XSendfile on the official nginx website

Note this transformation note.

My webroot directory is in/var/vhost/demo. the demo directory contains an uploadfiles folder, which stores the files uploaded by users. For example, if a user uploads a hello.docx file, the corresponding physical path should be/var/vhost/demo/xsendfile/uploadfiles/hello.docx. if we use the direct path "export readfile () method to load the file into the memory and forward it to the client. if the file is too large, your server may be suspended. Therefore, we need to improve the php download method, that is, the introduction describes how to use the XSendfile method of nginx to send files to the client. nginx already contains the senfile module by default, if nginx is installed. in the conf file, you will see "sendfile on;". "OK" indicates that you can directly use components that are not as complex as apache and re-load component compilation. Now, I paste my configuration file and code.

My nginx virtual host configuration file
Server {listen 80; server_name demo.markdream.com; root/var/vhost/demo; index. php; # This is to define that direct access to the beginning of the directory to read your file is not allowed only through 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 ;}}
A simple php file download script, downloads. php
 
Pleasant demo

Http://demo.markdream.com/xsendfile/downloads.php? Filenamehelhello.docx

Reference
  1. Http://wiki.nginx.org/XSendfile
  2. Http://www.laruence.com/2012/05/02/2613.html
  3. Http://kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.