简体   繁体   English

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

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

I have Wordpress website which is live.我有一个实时的 Wordpress 网站。 In this website are a lot of posts with uploads over 200GB.在这个网站上有很多上传超过 200GB 的帖子。 I want to create in localhost development environment which I have done, but I have problems with posts images, because I don't want to download all of them and put in my localhost.我想在我已经完成的本地主机开发环境中创建,但是我在发布图像时遇到问题,因为我不想下载所有这些并放入我的本地主机。 So I am wondering if there is any Nginx rewrite rule which will rewrite only domain name in my image urls.所以我想知道是否有任何 Nginx 重写规则会在我的图像 url 中只重写域名。

I need to rewrite this url:我需要重写这个网址:

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

To this one:对此:

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

This is possible with Nginx or there is other better solution? Nginx 可以做到这一点,还是有其他更好的解决方案?

Here are your options:以下是您的选择:

  • Use a redirection:使用重定向:

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

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

You can also use remote files only in case they are missing on the local development server:您也可以仅在本地开发服务器上缺少远程文件的情况下使用它们:

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