NGINX - performance HTTP server Support Forums

Please login or register.

Login with username, password and session length
Advanced search  

News:

Welcome to the Unofficial (YET) support forums for NGINX, a performance HTTP server.

Pages: [1]   Go Down

Author Topic: Using X-Accel-Redirect Header With Nginx to Implement Controlled Downloads  (Read 997 times)

0 Members and 1 Guest are viewing this topic.

katmai

  • Administrator
  • Jr. Member
  • *****
  • Karma: 0
  • Offline Offline
  • Posts: 57
    • View Profile
Sometimes you may need to implement controlled downloads when all downloads requests being sent to your script and then this script decides what to do: to send some file to the user or to show some access denied page or, maybe, do something else. In lighttpd server it can be done by returning X-Sendfile header from script. Nginx have its own implementation of such idea using X-Accel-Redirect header. In this short post I will try to describe how to use this feature from PHP and Rails applications.

Lets assume, that you have some site and using Apache with PHP or Rails for dynamic content on this site. If you will use nginx as reverse-proxy in front of your Apache server, you will reach two goals:

   1. You would be able free some resources on server while nginx will handle all slow requests to dynamic content (details are here).
   2. You would be able to implement controlled downloads of static files from site.


In this article I will assume, that site is located in /var/www directory and there are some static files (like movies or song or something else) in /var/www/files directory. Apache is listening on http://127.0.0.1:8080.

First of all, lets take a look at our nginx configuration:
Code: [Select]
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       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;

            proxy_buffer_size          4k;
            proxy_buffers              4 32k;
            proxy_busy_buffers_size    64k;
            proxy_temp_file_write_size 64k;

        }

        location /files {
            root /var/www;
            internal;
        }
    }
}Code: [Select]
<?php
// Get requested file name
$path $_GET["path"];

//...
// Perform any required security checks, validation
// and/or stats accounting
//...

// And redirect user to internal location
header("X-Accel-Redirect: /files/" $path);

?>

In Rails applications you can use following code in your controller:
Code: [Select]
// Get requested file name
path = @params["path"]

# ...
# Perform any required security checks, validation
# and/or stats accounting
# ...

# And redirect user to internal location
@response.headers['X-Accel-Redirect'] = "/files/" + path
Logged

zlzqq

  • Newbie
  • *
  • Karma: 0
  • Offline Offline
  • Posts: 16
  • Just started
    • View Profile


The world is becoming wow power leveling smaller due to globalization, migration, travel and the internet. Doing business worldwide is wow power leveling no longer the exclusive rights of multinational corporations. Many small or medium wow power leveling sized companies are fast realizing the opportunities of marketing and selling world of warcraft gold to global prospects and clients via the internet. Even those who concentrate on the local best wow gold market are awakening to the fact that their customers are no longer a homogeneous bunch, but have cheapest wow gold come from different parts of the world

Logged
Pages: [1]   Go Up
 

Page created in 0.033 seconds with 20 queries.