简体   繁体   中英

nginx X-Accel-Redirect with regex internal location causes 301

I have next problem with nginx. I setted up code to serve secured files. This is PHP code that redirects request:

$file_path = '';
$start = (isSet($_GET['start']) ? '?start='.$_GET['start'] : '');
if(check_url($org_url)){
    header("X-Accel-Redirect: /film/".$file_path.$start); die();
}
else {
    header("Location: /403.html");
    die();
}

and check_url function:

function check_url($org_url){
    global $file_path;
    ...
    $file_path = $_GET['file'];
    ...
    $hash = md5(...);
    if($url_time_to > time() && $hash === $url_hash){ return true; }
    else return false;
}

Then I have such nginx config:

location /file/ {
    rewrite . /file.php last;
}

location /file.php {
    internal;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location /film {
    alias /var/www/filmy;
    mp4;
    flv;
    internal;
}

and it all works. But I tried better (for me) config, where I can set separate directories inside config for different file types:

location /file/ {
    rewrite . /file.php last;
}

location /file.php {
    internal;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ /film/.*\.flv$ {
    internal;
    alias /var/www/filmy;
    flv;
}

location ~ /film/.*\.mp4$ {
    internal;
    alias /var/www/filmy;
    mp4;

Request to http://unexisting.com/file/....../54agda0g8.flv redirects me to http://unexisting.com/film/54agda0g8.flv/

God why? If you need live example let me know.

Nginx documentation says:

If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40)

So you need to have captures in regexp, like this:

location ~ ^/film/(.+\.flv)$ {
    internal;
    alias /var/www/filmy/$1;
    flv;
}

BTW, why do you want to split this config?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM