nginx X-Accel-Redirect与regex内部位置导致301


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

我有nginx的下一个问题。我设置了提供安全文件的代码。这是重定向请求的PHP代码:

$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();
}

和check_url函数:

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;
}

我有这样的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;
}

,它都工作。但是我尝试了更好的(对我来说)配置,我可以在配置中为不同的文件类型设置单独的目录:

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;

请求http://unexisting.com/file/....../54agda0g8.flv将我重定向到http://unexisting.com/film/54agda0g8.flv/

神为什么?

Nginx文档说:

如果在正则表达式定义的位置中使用别名,则该正则表达式应包含捕获,别名应引用这些捕获(0.7.40)

所以你需要在regexp中有捕获,像这样:

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

顺便说一句,你为什么要拆分这个配置?