Self portrait where I'm only visible from the eyes-up and hanging upside down from the top of the frame.

From the Desk of Brian Richards

A collection of thoughts and Ideas from Brian Richards, creator of WPSessions.com

Serve Missing Media from a Production Server via Apache/Nginx

When working on a website locally, it can become quite tedious to pull down all media from a production environment – particularly if you just need to spot-check one or two key areas. In some cases, the production sites I work with have media folders that are more than 1GB (!!) in size. In fact, it’s not entirely uncommon for a large site with a lot of history.

Several months ago I saw someone tweet a simple solution to this, but I didn’t take note of the URL anywhere and it quickly slipped into the ether. Googling for phrases like “apache redirect missing images to production server” or “serve missing images from remote server using apache” or “display live site images on development site” yielded unproductive results.

Then, today, while wishing once again for a solution to my dilemma, I found it!

To save others (and my future self) the struggle of trying to find this solution, I’m documenting it here.

In both of my examples below, make sure you swap example.com for your production server’s URL. And note that these snippets expect the URL structure to be the same between servers (they should be, if you’re working with a true staging copy of a production environment).

Here’s how to tell Apache to serve missing files via remote server:

Source: http://stackoverflow.com/questions/5130410/modrewrite-to-another-server-if-file-image-is-not-found

Note: If you’re working with WordPress, make sure this directive appears before the WordPress-generated rewrite rules in your .htaccess file. Otherwise, WP will catch the missing file URL and route it to a 404 internally.

And, here’s how to tell nginx the same thing:

Source: https://github.com/aliso/vvv-site-wizard/blob/master/vvv#L383

How about as a WordPress plugin?

You’re in luck! If you’re using WordPress and you’d prefer to slurp up those missing images (so you’re not always dependent on a remote server), you can use the plugin Uploads By Proxy. This plugin will fetch each missing image from the remote server and load it into your local uploads directory for future use. For my use cases, though, I just wanted to see the images on my staging site, not actually copy them and eat disk space.

Enjoy!


Posted

in

Comments

10 responses to “Serve Missing Media from a Production Server via Apache/Nginx”

  1. Daron Avatar
    Daron

    Thank you! None of the other tutorials or snippets I found mentioned placing the rewrite rules before WordPress’s rules. I guess I need to brush up on my server skills because I felt like an idiot when you mentioned it!

    Thanks again 😀

    1. Ryan Avatar
      Ryan

      Hahaha, same thing here brother! I thought, “yeah I can just jam all this in with the WordPress rules”. No.

  2. Mac Avatar
    Mac

    Thanks for also including the nginx configuration. All other guides I’ve read have only considered apache.

  3. […] however, it’s a much better idea to get the images on an as-needed basis. One way is to set up apache/nginx to load the media from the remote site for files that aren’t available locally. That method requires a little more knowledge and […]

  4. Maxim Pokrovskii Avatar

    Thanks for this tip.

  5. Brian DiChiara Avatar

    This is awesome! Thanks for posting it! For anyone else using the Nginx version with Local (Flywheel), the lines provided can be added in site.conf (conf/nginx/site.conf), inside the server directive, above `server_name` and `root` lines.

  6. Tanner Avatar

    Trying to get this running with Local (https://localwp.com/) and looking into creating an add-on for it. Thanks for the help! You saved me a ton of time.

  7. […] to this article, we don’t have to copy all the media library images. Instead, if something’s missing, […]

Leave a Reply

Your email address will not be published. Required fields are marked *