Sometimes you may need to control the download: the request for downloading the file is forwarded to a script, and then the script determines how to do it: send the file to the user, and the access page is rejected, or something else. On the Lighttpd server, the X-sendfile header can be returned from the script, while nginx is implemented by using the X-accel-redirect header. In this articleArticleI will try to briefly describe how to use this feature in PHP and rails.
Suppose you use Apache to run php or rails to generate dynamic content, and nginx is used as the reverse proxy at the front end (bianbian Note: reverse proxy is also called server accelerate ), the principle is to forward user requests to the target server and then forward the results to the user. There are many benefits: the security of the target server is protected, Server Load balancer is easy to implement, and it is a bit similar to the firewall. I think there are more steps to transfer the user's IP address ). You have achieved two goals:
- Because the nginx server will improve all slow requests to dynamic content, it can save server resources (details are being
Here ). (bianbian Note: Based on my understanding of nginx, nginx caches client requests and forwards them to the background script after all the requests are sent, for example, when uploading files. The advantage is that it reduces the waiting time of the background script and indeed improves the performance. The disadvantage is that the function of displaying the upload progress in the script is impossible. [of course, in the future, it is also possible for nginx to open this progress API by itself, but it is not script-level. Fortunately, the Progress display function is not commonly used.])
- You can control the download of static files. (bianbian note: this is the most important part in the next section !)
Here, assume that the website is located in the/var/WWW directory, while some static files (such as movies, songs, or others) are located in the/var/www/Files directory. Apache listens to port 8080.
First, let's take a look at the nginx Configuration:
- HTTP {
-
- ....
-
- Server {
-
- Listen 80;
-
- SERVER_NAME your-domain.com;
-
-
-
- Location /{
-
- Rewrite ^/download/(. *)/down. php? Path = $1 last;
-
-
-
- Proxy_pass http: // 127.0.0.1: 8080 /;
-
- Proxy_redirect off;
-
-
-
- Proxy_set_header host $ host;
- Proxy_set_header X-real-IP $ remote_addr;
-
- Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
-
-
-
- Client_max_body_size 10 m;
-
- Client_body_buffer_size 128 K;
-
-
-
- Proxy_connect_timeout 90;
-
- Proxy_send_timeout 90;
-
- Proxy_read_timeout 90;
-
-
- Proxy_buffer_size 4 K;
-
- Proxy_buffers 4 32 K;
-
- Proxy_busy_buffers_size 64 K;
-
- Proxy_temp_file_write_size 64 K;
-
-
-
- }
-
-
-
- Location/Files {
-
- Root/var/WWW;
-
- Internal;
-
- }
-
- }
-
- }
The keyword "internal" indicates which directories need to be switched internally with the background script through the X-accel-redirect header. Our script only needs to complete the download control. Other features such as multipart download are implemented by the nginx server like normal static files.
Here is the content of down. php:
-
- <? PHP
-
- // Get the file name to download
-
- $ Path=$ _ Get["Path"];
-
-
-
- //...
-
- // Check permissions and download statistics.
-
- //...
-
-
- // The redirection completes the download.
-
- Header("X-accel-redirect:/files/".$ Path);
-
- ?>
In rails, you can write the following in the controller:Code(Bianbian Note: rails is the agreed MVC Architecture ):
-
- # Get the file name to download
- Path=@ Params["Path"]
-
-
-
- #...
-
- # Permission verification and download statistics are completed here.
-
- #...
-
-
-
- # Redirect complete download
- @ Response.Headers['X-accel-Redirect']="/Files/"+Path
This is done! With the above method, we can create a flexible and extremely efficient file distribution system.
Original article: using X-accel-redirect header with nginx to implement controlled downloads (with rails and PHP examples)