How to configure nginx to redirect requests to the uploads directory to the production server?

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 Comment