Roots Discourse

Redirect image srcs from staging/production on local dev to save disk space?

I work on a Macbook Pro and manage a whole bunch of trellis/sage sites. Today I noticed that my available disk space is disappearing VERY quickly, because every time I set up a local dev, I have to rsync down all of the images from the staging/production sites in order for them to look/function right for development. For a lot of the devs I set up, I won’t even touch them for months afterwards until a new request comes through. I don’t want all these images hogging up all my disk space when I only need them every few months or a few times a year…

I did some searching and found some promising things. Some people said I could edit the /etc/nginx/nginx.conf file to have all missing images redirect from staging/production:

html {
  server {
    location ~* ^.+\.(svg|svgz|jpg|jpeg|gif|png|ico|bmp)$ {
      try_files $uri @image_fallback;
    }

    location @image_fallback {
      proxy_pass http://{PROD};
    }
  }
}

I haven’t been able to get this to work though. I add this, restart nginx with no errors, and there is no change and all images are still missing.

Someone else said to try a Chrome plugin to achieve this, but I couldn’t get that to work either.

Am I doing something wrong? Is there a different solution that is built to work with the Trellis framework? I don’t know much about nginx, I’m basically just copying and pasting and hoping something will stick…

Offload your media library! :smiley:

Hmmm… I’ve never thought about that possibility! That might be a good thing to do going forward for new sites we develop, but I’d still like to know how to redirect for all of the existing sites we’ve already built. We don’t exactly have the free time go through all these old sites and offload all of the images… Not immediately, anyway.

Could you delete the uploads folder for projects you’re not actively working on and then just rsync them down again when you do need them?

Funny, as I was just searching in my archive for a .htaccessI wrote some time ago to load content from the /uploads folder from the production server. However this is mainly for Apache and the file goes to /app/uploads/.htaccess. Maybe this is more interesting for Bedrock users…

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteBase /app/uploads/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*) https://LIVEDOMAIN.COM/app/uploads/$1 [L,P]

</IfModule>

However using a simple .htaccess-Converter (http://winginx.com/en/htaccess), this is the result:

# nginx configuration

location /app/uploads/ { 
    if (!-e $request_filename) { 
        rewrite ^/app/uploads/(.*) https://LIVEDOMAIN.COM/app/uploads/$1 redirect; 
    } 
}

Maybe you want to give it a try. I would be very interested if this might be useful.

Best

I needed a solution for this again as I came to another situation in which the production media library has around 40GB. And I really do not want to download that.

My solution is:

1.) Site specific configs are saved in /etc/nginx/sites-enabled
2.) Edit the specific file such as example.test.conf
3.) Add the following within server and before other locationrules. Don’t forget to set the live URL where to get the resources.

  # Directives to send expires headers and turn off 404 error logging.
  location ~* \.(png|jpe?g|gif|ico|svg)$ {
      expires 24h;
      log_not_found off;
      try_files $uri $uri/ @production;
  }

  location @production {
      resolver 8.8.8.8;
      proxy_pass https://example.com/$uri;
  }

This is not update proved. So I guess reprovision the local server will destroy the setting. Maybe @benword has a proper solution where to add this to let ansible add the rules.

Best,

Philipp

Rather than relying on Trellis, you can simply use PHP to filter the attachment URLs and replace the local domain with production domain, resulting in media files pointing to production files.

A simple MU plugin can be written to check the current environment and do the replacements as required.

Yes sure, that is possible, too. What do you think are the benefits if using PHP over a NGINX rule?