phpnginxmime-typesx-sendfilex-accel-redirect

Nginx: X-Accel-Redirect not working in files with know MIME extension


I am developing a webapp and X-Accel-Redirect header works fine only in files without extension. For some reason, if I add an extension to the file name the X-Accel-Redirect doesn't work.

Working example:

X-Accel-Redirect: /protected_files/myfile01.z

Non-working example:

X-Accel-Redirect: /protected_files/myfile01.zip

I'm using nginx 1.7.1.

Initially, The weird part is that if I change the extension part (in this case ".zip") with something not registed in the mime.types file, it works fine (Obviously I rename the file accordingly), but with a extension pointing to a know mime type (something like "zip", "jpg", "html") will generate a "404 Not found" error.

UPDATE: It seems that the issue is due to this rule I have in the conf file:

location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    try_files $uri =404;
}

For some reason, it seems that nginx tests the existence of the file in the file system first and after that it tries the the "internal/aliased" path.

Any ideas about how to let nginx to filter all the "/protected_files" coming from X-Accel-Redirect directly to the "internal" instead of trying to find in other paths first?

Thanks in advance.


Solution

  • The error was due to a conflict in rules in nginx config file.

    So, the solution was:

    location ^~ /protected_files { # ^~ needed according to the [nginx docs][1] to avoid nginx to check more locations
        internal;
        alias /path/to/static/files/directory;
    }
    
    #avoid processing of calls to unexisting static files by my app
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
    

    Hope this helps many of you.