Use PHP as a Proxy to Dropbox for Serving a Static Website

Ever wanted to use Dropbox for managing your static HTML site or blog? One option is to simply install the Dropbox Linux client and symlink the folder you want to the public folder server by Apache or Nginx on your server. Another option is to use a simple PHP script as a proxy to your Dropbox folder with additional Nginx caching. The idea is to have something similar to Drapache.

Why PHP?

Dropbox requires OAuth for all API requests and you need a way to parse those responses from the Dropbox File API. With additional layer of Nginx or APC or Memcache caching we can reduce the amount of requests to the Dropbox API and make it much faster.

Sample PHP Script and Nginx Configuration for Caching

Here is the index.php file that will be doing all the “proxying” on your actual web server:

$keys = array(
        'app_key' => '',
        'app_secret' => '',
        'consumer_key' => '',
        'consumer_secret' => ''
);

$context = stream_context_create( array(
        'http' => array(
                'method' => 'GET',
                'header' => sprintf( 
                        'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="%s", oauth_token="%s, oauth_signature="%s&%s"',
                        $keys['app_key'],
                        $keys['consumer_key'],
                        $keys['app_secret'],
                        $keys['consumer_secret']
                )
        )
) );

// Get the current request URI
$request = parse_url( $_SERVER['REQUEST_URI'] );

// Use only the actual path and exclude query args
$uri = $request['path'];

// Return an index page if we are requesting a folder
if ( substr( $uri, -1 ) == '/' )
        $uri .= 'index.html';

// Get response from Dropbox
$response = file_get_contents( 'https://api-content.dropbox.com/1/files/dropbox/Apps/droproxy' . $uri, false, $context );

// Forward headers returned by Dropbox
foreach ( $http_response_header as $header )
        header( $header );

// Return file content
echo $response;

And here is a sample Nginx server block that will cache those responses:

fastcgi_cache_path  /var/tmp/cache  levels=1:2
                    keys_zone=droproxy:10m
                    inactive=5m;

server {
        server_name  droproxy.konstruktors.com;

        root  /var/www/vhosts/$host/public;

        index  index.php;
        try_files  $uri /index.php;

        location /index.php {
                fastcgi_pass  php;
                include  fastcgi_params;

                fastcgi_cache  droproxy;
                fastcgi_cache_key  $scheme$request_method$host$uri;

                fastcgi_ignore_headers  Cache-Control Expires;

                fastcgi_cache_valid  404 1m;
                fastcgi_cache_valid  any 5m;

                fastcgi_no_cache  $arg_nocache;
                fastcgi_cache_bypass  $arg_nocache;
        }

}

Demo

You can see it working here: droproxy.konstruktors.com

Leave a Reply