Tuesday, September 13, 2022

nGinx rewrite rules configuration

 The rewrite Directive



rewrite regex URL [flag];

eg:

rewrite ^ $request_uri;

server {

    # ...

    rewrite ^(/download/.*)/media/(\w+)\.?.*$ $1/mp3/$2.mp3 last;

    rewrite ^(/download/.*)/audio/(\w+)\.?.*$ $1/mp3/$2.ra  last;

    return  403;

    # ...

}

Explanation:

 ->It matches URLs that begin with the string /download 

  -> Then include the /media/ or /audio/ directory somewhere later in the path.

->   It replaces those elements with /mp3/ and adds the appropriate file extension, .mp3 or .ra

Example,

/download/cdn-west/media/file1 becomes /download/cdn-west/mp3/file1.mp3. 

If there is an extension on the filename (such as .flv), the expression strips it off and replaces it with .mp3

No comments:

Post a Comment