【问题标题】:proxy pass and url rewriting in ngnixnginx中的代理传递和url重写
【发布时间】:2015-12-27 01:50:58
【问题描述】:

使用 proxy_pass 转发请求。 如果输入的 url 是 www.yyy.com/9.157/7.134/live/playlist.m3u8

我想将它代理传递给 10.5.a.b:1935/live/playlist.m3u8 ,

在前两个八位组 (10.5) 保持不变的情况下,我需要做的就是从 url 中提取 9.157 和 7.134 并将其传递给 10.5.ab:1935 或(如果 10.5.9.157:1935 下降 proxy_pass 到 10.5 .7.134:1935 )

这就是我的 nginx 配置的样子

位置/{

       rewrite (\/)(([0-9][0-9][0-9]|[0-9][0-9]|[0-9])\.([0-9][0-9][0-9]|[0-9][0-9]|[0-9]))(\/)  http://10.5.$2:1935/live/suhas_712_media_240p/playlist.m3u8  redirect;

}

上面的代码正在运行,但是我不想重定向,我想做一些类似下面的事情

proxy_pass 10.5.a.b:1935

如何将提取的值传递给 a,b?

谢谢

【问题讨论】:

    标签: nginx rewrite proxypass


    【解决方案1】:

    这应该有效:

    (为了清楚起见,我使用了regex names

       location ~* "/(?<a>[0-9]{1,3}\.[0-9]{1,3})/(?<b>[0-9]{1,3}\.[0-9]{1,3})/(?<suffix>.+)$" {
            set $port 1935;
            set $prefix "http://10.5";
            set $target_url_a "$prefix.$a:$port/$suffix";
            set $target_url_b "$prefix.$b:$port/$suffix";
            add_header X-debug-message_a "$target_url_a"; #just testing   
            add_header X-debug-message_b "$target_url_b"; #just testing             
            #proxy_pass $target_url_a; #enable this one for real
            return 200; #just testing        
        }
    

    curl -vv http://localhost/9.157/7.134/live/playlist.m3u8

    > GET /9.157/7.134/live/playlist.m3u8 HTTP/1.1
    ...
    > User-Agent: curl/7.43.0
    > Accept: */*
    >
    
    < HTTP/1.1 200 OK
    ...
    < Content-Length: 0
    < Connection: keep-alive
    < X-debug-message_a: http://10.5.9.157:1935/live/playlist.m3u8
    < X-debug-message_b: http://10.5.7.134:1935/live/playlist.m3u8
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 2012-05-19
      • 2018-05-06
      • 1970-01-01
      • 2014-02-01
      • 2016-11-15
      • 2018-04-06
      相关资源
      最近更新 更多