【问题标题】:Nginx rewrite rule for changing domain name in image urlsNginx 修改图片url中域名的重写规则
【发布时间】:2020-11-23 16:21:44
【问题描述】:

我有实时的 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 实现还是有其他更好的解决方案?

【问题讨论】:

    标签: wordpress nginx


    【解决方案1】:

    以下是您的选择:

    • 使用重定向:

      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
    }
    

    【讨论】:

    • 据我了解,应该在实时服务器上启用透明代理,对吧?如果是这样,那么你能告诉我 Apache 的语法吗,因为它正在运行实时网站。
    • @RydelHouse 不,此配置应该在您的本地主机上使用,您可以在其中使用 nginx 提供本地 WordPress 站点副本。 nginx 可以对文件请求发出重定向(第一个配置)或从实时服务器本身获取文件(第二个配置)。
    猜你喜欢
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2015-07-03
    • 2012-11-27
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多