繁体   English   中英

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

[英]Nginx rewrite rule for changing domain name in image urls

我有一个实时的 Wordpress 网站。 在这个网站上有很多上传超过 200GB 的帖子。 我想在我已经完成的本地主机开发环境中创建,但是我在发布图像时遇到问题,因为我不想下载所有这些并放入我的本地主机。 所以我想知道是否有任何 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM