How-to use Media Vault with Nginx


The Media Vault plugin for WordPress is the only WordPress plugin that I’m aware of that allows you to protect uploaded files from public access. Protecting your WordPress web site is relatively easy and there a bunch of great plugins¬†that help make your site private for internal use.

But there is a bit of a catch, the Media Vault plugin only supports Apache web servers. So if you’re running your WordPress installation on Nginx, there’s a small hack you need in order to make it work.

Step 1 – Install and modify the plugin

First of all install the Media Vault plugin like you normally would, and it will give you a message that you need to activate the Apache rewrite rules. Of course it won’t work, so in order to activate the plugin we’ll do this.

  1. Under WordPress Admin go to Plugins / Editor to edit the Media Vault plugin.
  2. From the “Select plugin to edit:” menu select “Media Vault” and click Select
  3. On the right-hand side click on the media-vault/_mediavault.php plugin file.
  4. Look for a line that reads: “function mgjp_mv_check_rewrite_rules( $deactivation = false ) {
  5. Modify the return statement in that function so it reads: return true;

That simply makes the activation successful even though we don’t actually have it working yet. The reason we have to do this is because the nginx rewrite rules that we’re going to add work, but don’t pass the test in this function because they don’t return the same http codes that apache does.

Step 2 – Configure Nginx

Here’s what my relevant nginx configuration looks like, you can copy parts as needed:

location / {
    rewrite ^/wp-content/uploads(/_mediavault/.*\.\w+)$ /index.php?mgjp_mv_file=$1 last;
    if ($args ~* "^(?:.*&)?mgjp_mv_download=safeforce(?:&.*)?$") {
        rewrite ^/wp-content/uploads(/.*\.\w+)$ /index.php/?mgjp_mv_file=$1 last;
    }
 
    try_files $uri $uri/ @rewrites;
}

location @rewrites {
    rewrite ^ /index.php last;
}

location /wp-content/uploads {
    rewrite /wp-content/uploads/([1-9]+.+) /wp-content/uploads/_mediavault/$1 redirect;
}

This configure takes care of WordPress pretty-urls, Media Vault rules, and also redirects any old upload URLs from your media library to the new media vault protected URL. This was helpful in our case were we already had a lot of existing media in use in the WordPress site and it would have taken a lot of effort to update all the URLs on the pages.

Hope this helps someone out there, enjoy!


Leave a Reply

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