Forward requests using proxy_pass. If url typed is www.yyy.com/9.157/7.134/live/playlist.m3u8
I want to proxy_pass it to 10.5.a.b:1935/live/playlist.m3u8 ,
where the first two octets (10.5) remain constant , all i need to do is extract 9.157 and 7.134 from the url and proxy pass it to 10.5.a.b:1935 or (if 10.5.9.157:1935 is down proxy_pass to 10.5.7.134:1935 )
this is how my nginx config looks
location / {
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;
}
the above code is working but then i do not want to redirect, i want to do something like following
proxy_pass 10.5.a.b:1935
how to pass extracted values to a,b?
Thanks
This should work:
(I used regex names for clarity)
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