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

1

5

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.

Isadora answered 3/6, 2014 at 15:21 Comment(1)
Btw, I already took a look at similar questions at SO and didn't see any similar problem.Isadora
I
11

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.

Isadora answered 3/6, 2014 at 16:28 Comment(3)
What is the solution exactly? That code is also on the question.Monreal
@MichaelRogers the solution was to add the section: location ^~ /protected_filesIsadora
Thank you! Saved me a couple more hours bashing (pun) away at this thingAscent

© 2022 - 2024 — McMap. All rights reserved.