【问题标题】:using proxy_pass with dynamic variables nginx使用带有动态变量 nginx 的 proxy_pass
【发布时间】:2017-01-16 20:53:10
【问题描述】:

我正在尝试使用 proxy_pass 和 nginx 来屏蔽重定向到我的图像 CDN。我希望能够走上这样的道路:

myserver.com/images/12345/whatever-name-goes-here.jpg

我想代理到

http://imagecdn.com/12345.jpg

我已经尝试了以下

location ~ /images/(.*)/(.*) {
      proxy_pass http://imagecdn.com/$1.jpg; 
    }

但我不断收到 502 错误。知道这是否可能吗?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    我建议使用实际的重定向,例如:

    location ~ ^/images/(.*)/(.*)$ {
        return 301 $scheme://imagecdn.com/$1.jpg;
    }
    

    【讨论】:

    • 我想我看到了问题,当位置是正则表达式时,您不能以这种方式使用 proxy_pass,请参阅proxy_pass 的文档并向下滚动到“在某些情况下”。您必须使用重写规则:location /images { rewrite ^/images/(.*)/(.*)$ $1.jpg break; proxy_pass http://imagecdn.com;}
    • 这是我一直在寻找的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2022-11-03
    • 2013-06-04
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多