django-sendfile icon indicating copy to clipboard operation
django-sendfile copied to clipboard

nginx X-Accel-Redirect requires unquoted URLs.

Open stuxcrystal opened this issue 8 years ago • 5 comments

As the title says, nginx X-Accel-Redirect expects an unquoted string.

Therefore quoting the URL in the nginx backend will cause internal 404s.

stuxcrystal avatar Oct 15 '16 11:10 stuxcrystal

I've been having this issue myself too. It was added this way to support unicode characters in the URIs in https://github.com/johnsensible/django-sendfile/pull/54. For now I am using django-sendfile==0.3.10 to workaround the issue for characters such as ! but Unicode characters will raise an exception when Django tries to write the headers.

I'm wondering what nginx and django version, decoded the URI correctly for you @Proper-Job , when you wrote the patch, if you don't mind helping out?

My Django is 1.8.16 and my nginx is 1.4.6

willstott101 avatar Nov 22 '16 12:11 willstott101

@willstott101 @stuxcrystal I have 0.3.11 deployed on nginx 1.6.2 and django 1.8.14 with unicode and non-unicode filenames without any 404 exceptions. Does downgrading to 0.3.10 fix your issue without touching the server config?

Proper-Job avatar Nov 22 '16 13:11 Proper-Job

Thanks @Proper-Job

0.3.10: Ascii characters such as ! are served with sendfile fine. Unicode characters such as Б fail with a UnicodeEncodeError when gunicorn writes the request to the socket (just noticed this, I assumed it was django thing before).

0.3.11: Ascii and unicode characters 404, and from the nginx log it looks like it's looking for a file with the literal, escaped URL. Such as /var/opt/.../hmm/%21.png

I'll see if I can test with a newer version of nginx. This is all on my development machine, Ubuntu 14.04

willstott101 avatar Nov 22 '16 13:11 willstott101

I switched to a PPA and installed nginx 1.11.5 and it's working flawlessly with sendfile 0.3.11, so it appears the version of nginx available on the official ubuntu trusty repos is too old to decode the header.

And this page: http://nginx.org/en/CHANGES seems to have the change pinned to version 1.5.9 Ctrl+F: escaped URIs in "X-Accel-Redirect"

As to what should be done by this library (if anything) to support older versions of nginx is a different issue. Maybe as a setting? But that smells a bit to me.

willstott101 avatar Nov 22 '16 14:11 willstott101

@willstott101 nice digging!

Proper-Job avatar Nov 22 '16 14:11 Proper-Job