Access to production files without local storage on dev

Public

If you are developing on a website already in production, you generally need to get all the /files/ repository to have access to all your "user uploaded" content.

Methods below allow you to have access to both the files from production and locally files without the need of download all distant resources.

You need to add the following instructions into your Vhost.

Get raw version
php
  1. /* For Nginx server */
  2.  
  3. // Testing all files on /files/ repository
  4. // but exclude css and js to prevent errors.
  5. location ~ /sites/.*/files/.*(?!css|!js)$ {
  6. error_page 404 = @fetch;
  7. }
  8.  
  9. // Redirect to prod URL.
  10. location @fetch {
  11. proxy_pass http://www.example.com;
  12. }

Same for Apache

Get raw version
php
  1. /* For Apache server
  2. * In that example, we are not dealing with css and js risks as previous one.
  3. */
  4.  
  5. RewriteCond %{REQUEST_URI} ^/sites/default/files/
  6. RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
  7. RewriteRule ^/(sites/default/files/.*) http://www.example.com/$1 [L]

Drupal's module alternative

Get raw version
php
  1. http://drupal.org/project/stage_file_proxy

Comments

misterjoonas's picture

Do you have any experience using this with varnish? I got the stage_file_proxy working on my local, which is not running varnish usually, but on the development server we have varnish and all the pictures generate a server 500. Both with stage_file_proxy and nginx conf trick.

Christine's picture

Hi! Thanks it works really well, only thing is now I can't login because of files persmissions. Any suggestions?

Stijn Huyberechts's picture

You should can use the Apache proxy module (mod_proxy and mod_proxy_http) to proxy pass the request instead of redirecting. That way it acts similar to the nginx solution. So enable mod_proxy_http (it also enables mod_proxy) if not already and change the rewrite rule flags:

RewriteRule ^/(sites/default/files/.*) http://www.example.com/$1 [P,QSA]

Note the QSA flag to append the request query string to the proxy request uri as well. The P (proxy) flag implies the L flag, so you can leave it out.