This is tested for WordPress and assumes there is no hot-link protection in place.
One of the painpoint I had for the past decade was: how to create a local development of a live environment without copying all the files locally. Sometimes copying files is not really the best option as it will use lots of disk space (one of my clients uses around 40GB of images and documents). So I kept searching for a solution. Here is what I’ve found.
Apache
In order to make Apache play nice, you need to edit the vhost entry:
<VirtualHost *:80> // or 443 if it's https
Redirect permanent "/wp-content/uploads/" "http://live-url/wp-content/uploads/"
....etc
Then, create a file in wp-content/uploads/.htaccess
:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://live-url/wp-content/uploads/$1 [L]
Since I didn’t used Apache in the past 5+ years, I never bother to customize it further for other usecases (e.g. multisite)
Nginx
Include this in your sites-enabled/my-site.conf
:
location @proxy {
proxy_ssl_server_name on;
proxy_http_version 1.1;
proxy_set_header Accept-Encoding "";
proxy_set_header Cache-Control no-cache;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
add_header X-query-string $is_args$query_string;
resolver 0.0.0.0;
resolver_timeout 5s;
# REPLACE THIS URL
proxy_pass https://the-live-site-url.tld;
}
location /wp-content/uploads/ {
log_not_found off;
access_log off;
try_files $uri $uri/ @proxy;
}
# for multisite
location /wp-content/blogs.dir/ {
log_not_found off;
access_log off;
try_files $uri $uri/ @proxy;
}
# for bedrock
location /app/uploads/ {
log_not_found off;
access_log off;
try_files $uri $uri/ @proxy;
}
Caveats
There are a couple of caveats with using this:
File Stats
Any file stats will not be available. So file size, file meta etc. (these are fetched on the fly by WordPress, not stored in DB).
File Operations
Also, any file operation won’t be available: resizing, generating previews, deleting etc. Since the files are not there, it’s kind of obvious that won’t work, right?
Lasă un răspuns