Apache, nginx, and Lighttpd use the X-sendfile function to improve file download performance.

Source: Internet
Author: User
Tags sendfile

 

About mod_xsendfile

Https://tn123.org/mod_xsendfile/

 

X-sendfile in Lighttpd

How the ROR website uses the X-sendfile function of Lighttpd to improve File Download Performance

In the X-sendfile mode, server applications do not need to read the downloaded files. You only need to set the response header information, in addition, you need to append the "x-Lighttpd-send-file" information to Lighttpd, telling Lighttpd that the file download will not work.

 

X-sendfile in Apache

Allows PHP to provide faster file downloads

Without passing through the PHP layer, the webserver directly sends the file, and uses the Apache module mod_xsendfile to allow Apache to directly send the file to the user.

<? PHP $ file = "/tmp/Chinese name .tar.gz"; $ filename = basename ($ file); header ("Content-Type: Application/octet-stream "); // process the Chinese file name $ UA = $ _ server ["http_user_agent"]; $ encoded_filename = urlencode ($ filename); $ encoded_filename = str_replace ("+", "% 20 ", $ encoded_filename); If (preg_match ("/MSIE/", $ UA) {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. '"');} header ('content-Disposition: attachment; filename = "'. basename ($ file ). '"'); // Let xsendfile send the file header (" X-sendfile: $ file ");

 

 

X-accel-redirect in nginx

Configure xsendfile in nginx to improve File Download Performance

The header feature called X-sendfile is used to send static files.
Nginx also has this feature, but the implementation is slightly different. It is called X-accel-redirect in nginx.

There are two features that need to be elaborated:
1. the header must contain the URI
2. The local file must be declared as internal for internal redirect and X-accel-redirect responses.

Configuration example:
Code
Location/public /{
Internal;
Root/project directory;
}

Application interface:
Code
X_accel_redirect "/project directory/public",: filename => "ISO. IMG"

In this way, nginx will send the/project directory/public/ISO. IMG file.
Other examples:
Reference
X_accel_redirect ('/path/to/image.jpg',: TYPE => 'image/JPEG ',: Disposition => 'line ')

Rails x_accel_redirect plug-in
Http://github.com/goncalossilva/X-Accel-Redirect

The header also supports the following attributes:
Code
X-accel-limit-rate: 1024
X-accel-buffering: Yes | No
X-accel-charset: UTF-8

 

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.