Nginx重写规则,用于更改图像URL中的域名



我有Wordpress网站,它是实时的。在这个网站上有很多上传量超过200GB的帖子。我想在localhost开发环境中创建,我已经完成了这一点,但我在发布图像时遇到了问题,因为我不想下载所有图像并放在我的localhost中。所以我想知道是否有任何Nginx重写规则,它将只重写我的图像URL中的域名。

我需要重写这个网址:

https://example.local/wp-content/uploads/2020/10/10/very-nice-picture.png

对此:

https://example.com/wp-content/uploads/2020/10/10/very-nice-picture.png

这是可能的Nginx或有其他更好的解决方案?

以下是您的选项:

  • 使用重定向:

    location /wp-content/uploads/ {
    return 301 http://example.com$request_uri; # or use 302 to prevent redirection caching
    }
    
  • 使用透明代理:

    location /wp-content/uploads/ {
    proxy_pass http://example.com;
    }
    

只有在本地开发服务器上丢失远程文件时,您才能使用远程文件:

location /wp-content/uploads/ {
try_files $uri @remote;
}
location @remote {
# redirect or proxy the request as shown above
}

最新更新