|
|
Posted on 11/7/2018 10:05:15 AM
|
|
|

BBS or websites often have only you have permission to see this page, or download this resource, a few days ago I visited the nginx server how to implement this file control, using x-sendfile.
1. What is X-Sendfile?
X-Sendfile is a mechanism that relays file download requests from the backend application to the front-end web server for processing, which can significantly improve server efficiency by removing the pressure of the back-end program to read and process the file to process the send, especially when dealing with large file downloads.
X-Sendfile is implemented through a specific HTTP header: specifying the address of a file in the X-Sendfile header to advertise to the front-end web server. When the web server detects this header sent by the backend, it ignores other output from the backend and uses its own components (including optimizations such as cache headers and breakpoint reconnections) to send the file to the user.
However, before using X-Sendfile, it's important to understand that this is not a standard feature and is disabled by most web servers by default. Different web servers have different implementations, including different X-Sendfile header formats. If misconfigured, users may download a 0-byte file.
Using X-Sendfile will allow downloading files in non-web directories (e.g. /root/) even if the file is disabled under .htaccess protection.
Different web servers implement different HTTP headers
SENDFILE header | WEB server used | | X-Sendfile | Apache, Lighttpd v1.5, Cherokee | | X-LIGHTTPD-send-file | Lighttpd v1.4 | | X-Accel-Redirect | Nginx, Cherokee |
The downside of using X-SendFile is that you lose control of the file transfer mechanism. For example, if you want to perform certain actions after downloading a file, such as allowing the user to download the file only once, this X-Sendfile will not be able to do so because the php script in the background does not know if the download is successful.
2. How to use NGINX?
Nginx supports this feature by default and does not require additional modules to be loaded. It's just that the implementation is a little different, and the HTTP header that needs to be sent is X-Accel-Redirect. In addition, you need to make the following settings in the configuration file
internal means that this path can only be accessed within Nginx and cannot be accessed directly by the browser to prevent unauthorized downloads.
3. How to use PHP programs?
If you add the X-Accel-Redirect header, the user will download the file under the /game path. Our file permission control is implemented.
Official documentation introduction:The hyperlink login is visible.
|
Previous:Love, marriage and family, first half of the month + June 18Next:CF is the first perspective on the whole network to self-aim and stabilize for one month
|