Magento 2 – X-Sendfile / X-Accel-Redirect headers for downloadable products

Magento 2 X-Sendfile / X-Accel-Redirect headers
When you have a Magento 2 store which server large files you may encounter the issue where download process breaks after 1GB of file. This issue may be solved if you will use X-Sendfile / X-Accel-Redirect headers for Apache and NGINX web servers.

What are X-Sendfile / X-Accel-Redirect headers?

The X-Accel-Redirect header for NGINX allows for internal redirection to a location which will be sent in the header. It would allow you to handle logging/authentication at your backed and NGINX will serve a content from redirected location for the end user.
For example:

At the example above we will redirect download to /protected_files location where additional rules can be set at NGINX.

The X-Sendfile header used for Apache web server. In order to use it you need to have installed mod_xsendfile at the your server. The X-Sendfile allows you to use PHP to instruct the server to send a file to a user, without having to load that file into PHP. It can also solve a problem of large files downloads and file access. Same as with NGINX you can store file outside of document root and have it inaccessible for visitors.

In order to use it you will need to add to your httpd.conf an X-Sendfile section that looks like:

Magento 2 X-Sendfile / X-Accel-Redirect headers for downloadable products

We had reviewed the Magento 1.x extension which looks similar to what we want to implement for Magento 2.x. Unfortunate there are no extension which does the same for Magento 2 and we have created one!
Magento 2 X-Sendfile / X-Accel-Redirect headers

The extension adds X-Sendfile / X-Accel-Redirect headers for downloadable products at Magento 2. Once you have it installed you will have to open Stores->Configuration->Sashas Extensions->XsendFile.
Magento 2 X-Sendfile / X-Accel-Redirect headers

There you will have few options:
Magento 2 X-Sendfile / X-Accel-Redirect headers

You can disable/enable X-Sendfile / X-Accel-Redirect headers for your server. When you select server the correct X-Sendfile / X-Accel-Redirect header will be sent to the browser when customer going to download a product.
You can also Bypass MIME detection, but it will work only for file-based downloads.
If you are using NGINX server then you will be able to set “Alias path” which will be the path you have at your NGINX website config file.

Leave a Reply

Your email address will not be published. Required fields are marked *