Welcome! Log In Create A New Profile

Advanced

Using proxy_redirect when the location could be a range of values or unknown

Posted by chalky 
Using proxy_redirect when the location could be a range of values or unknown
September 14, 2012 07:27AM
Hi, I am making an web app using SoundCloud's API. Requesting an MP3 to stream involves two requests. I'll give an example. Firstly:

http://api.soundcloud.com/tracks/59815100/stream

This returns a 302 with a temporary link to the actual MP3 (which varies each time), for example:

http://ec-media.soundcloud.com/xYZk0lr2TeQf.128.mp3?ff61182e3c2ecefa438cd02102d0e385713f0c1faf3b0339595667fd0907ea1074840971e6330e82d1d6e15dd660317b237a59b15dd687c7c4215ca64124f80381e8bb3cb5&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1347621419&Signature=Usd%2BqsuO9wGyn5%2BrFjIQDSrZVRY%3D

The issue I had was that I am attempting to load the MP3 via JavaScript, and for security reasons the browser can't follow the 302, as ec-media.soundcloud.com does not contain the correct header.

So instead of using the SoundCloud URL, I set up two locations in nginx, so the browser only interacts with the server my app is hosted on and no security errors come up:

location /soundcloud/tracks/ {
rewrite \/soundcloud\/tracks\/(\d*) /tracks/$1/stream break;
proxy_set_header Host api.soundcloud.com;
proxy_pass http://api.soundcloud.com;
proxy_redirect http://ec-media.soundcloud.com /soundcloud/media;
}

location /soundcloud/media/ {
rewrite \/soundcloud\/media\/(.*) /$1 break;
proxy_set_header Host ec-media.soundcloud.com;
proxy_pass http://ec-media.soundcloud.com;
}

So myserver/soundcloud/tracks/59815100 returns a 302 to /myserver/soundcloud/media/xYZk0lr2TeQf.128.mp3...etc, which then forwards the MP3 on.

This works! However, I have hit a snag. Sometimes the 302 location is not ec-media.soundcloud.com, it's ak-media.soundcloud.com. There are possibly even more servers out there and presumably more could appear at any time. Is there any way I can handle an arbitrary 302 location without having to manually enter each possible variation?

Or is it possible for nginx to handle the redirect and return the response of the second step? So myserver/soundcloud/tracks/59815100 returns the MP3?

The browser automatically follows the redirect, so I can't do anything with the initial response on the client side.

I am new to nginx and in a bit over my head so apologies if I've missed something obvious, or it's beyond the scope of nginx. Thanks a lot for reading.



Edited 3 time(s). Last edit at 09/14/2012 07:32AM by chalky.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 170
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready