I’m just getting my feet wet with nginx. In the past I’ve used an .htaccess file in /wp-content/uploads so if my dev or staging server doesn’t have the file it redirects to the production server:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteBase /wp-content/uploads/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*) http://production.server.com/m/wp-content/uploads/$1 [L,P]

</IfModule>

I’m not having luck with doing this in nginx. It may be in part because this particular time my site is in a subdirectory (/m/).

# Tells nginx which directory the files for this domain are located
root         /srv/www/example/htdocs;
index               index.php;

    # Subdirectory location settings

    location /m {
            index index.php;
            try_files $uri $uri/ /m/index.php?$args;
            location /m/wp-content/uploads {
                    try_files $uri $uri/ @prod_svr;
            }
    }
    location @prod_svr {
            proxy_pass http://production.server.com/m/wp-content/uploads$uri;
    }

Any ideas would be greatly apprciated.

2 s
2

You could try something like this:

server {
    root /srv/www/example/htdocs;
    index index.php;

    # Matches any URL containing /wp-content/uploads/    
    location ~ "^(.*)/wp-content/uploads/(.*)$" {
        try_files $uri @prod_serv;
    }

    # Will redirect requests to your production server
    location @prod_serv {
        rewrite "^(.*)/wp-content/uploads/(.*)$" "http://yourdomain.com/m/wp-content/uploads/$2" redirect;
    }

    # The rest of your location blocks...
    location /m {
        index index.php;
        try_files $uri $uri/ /m/index.php?$args;
    }
}

Leave a Reply

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