【问题标题】:Reverse image proxy without specifying host不指定主机的反向图像代理
【发布时间】:2017-11-21 03:52:02
【问题描述】:

我的配置中有以下内容作为图像的反向代理:

location ~ ^/image/(.+) {
    proxy_pass http://example.com/$1;
}

问题在于并非所有图片都是 example.com 图片,因此我们需要传入完整的 url。如果我尝试:

location ~ ^/image/(.+) {
    proxy_pass $1;
}

我收到一个错误:

invalid URL prefix in "https:/somethingelse.com/someimage.png"

【问题讨论】:

  • 当它指向其他域时,您将使用的 url 是什么?

标签: nginx reverse-proxy nginx-reverse-proxy


【解决方案1】:

这个问题很模糊,但是,根据错误消息,您要做的是完全根据用户输入执行proxy_pass,方法是使用/image/ 前缀之后指定的完整 URL URI。

基本上,这是一个非常糟糕的主意,因为您正在开放自己以成为开放代理。但是,它不像您提供的conf那样工作的原因是由于URL规范化,在您的情况下,它将http://example压缩成http:/example(双斜杠变成单斜杠),这在@的上下文中是不同的987654329@.

如果您不关心安全性,您可以将merge_slashes 从默认的on 更改为off

merge_slashes off;
location …

另一种可能是和nginx proxy_pass and URL decoding有些关系

location ~ ^/image/.+ {
    rewrite ^ $request_uri;
    rewrite ^/image/(.*) $1 break;
    return 400;
    proxy_pass $uri; # will result in an open-proxy, don't try at home
}

正确的解决方案是实施白名单,可能借助 map 甚至基于前缀的位置指令:

location ~ ^/image/(http):/(upload.example.org)/(.*) {
    proxy_pass $1://$2/$3;
}

请注意,根据开头的解释,上面的位置受merge_slash设置的约束,因此,默认情况下它永远不会有双//,因此需要添加双@ 987654339@ 手动在proxy_pass 阶段。

【讨论】:

  • 这是一个很好的点 re:open 代理。我计划确保它是一个图像(例如以.png、.jpg 等结尾)。还按照此处endpoint.com/blog/2016/05/25/… 的描述实现安全链接模块。这些方法是否足以让它在生产中使用?
  • @kristen Content-Type 响应标头告诉您所服务内容的 MIME 类型;不知道 DDG 是如何做到的,我认为他们的逻辑不仅仅是 nginx.conf。 :-) TBH,如果我要阻止打开代理的一种文件类型,图像可能就是它,因此,将图像列入白名单几乎不会增加任何保护。使用secure_link 可能会解决安全问题,但前提是正确实现整个逻辑(例如,仅根据受信任用户的输入生成链接)。
【解决方案2】:

在这种情况下我会使用地图

map $request_uri  $proxied_url {
   # if you don't care about domain and file extension
   ~*/image/(https?)://?(.*)   $1://$2;

   # if you want to limit file extension
   ~*/image/(https?)://?(.*\.(png|jpg|jpeg|ico))$   $1://$2;
   # if you want to limit file extension and domain

   ~*/image/(https?)://?(abc\.xyz\.com/)(.*\.(png|jpg|jpeg|ico))$   $1://$2$3;
   default "/404";
}

然后在您的代理通行证部分,您将使用如下所示的内容

location /image/ {
   proxy_pass $proxied_url;
}

我给出了三个不同的示例,具体取决于您要如何处理它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2015-11-22
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多